AccessVBA覚書 郵便番号から住所を出す(MsYubin7.dll)

Accessで住所支援入力というのがあるが、プロパティで設定せずにロジックで行うにはどうしたらよいかという問題への対応策。
使うのは住所支援入力で使っているライブラリと同じもの(だとは思う)。MsYubin7.dll。

3つのテキストボックスPrefText、CityText、TownTextに、都道府県、市区町村、町域をそれぞれ設定する前提で。

まず呼出元。

Dim res() As String
ConvZip2arrAddr("1600005",res)
PrefText = res(0)
CityText = res(1)
TownText = res(2)

まず呼出先。これは別モジュールを作成して記載しておけばいい。

Private Declare PtrSafe Function zcGetZipDecision Lib "MSYubin7.dll" Alias "GetZipDecision" _
                                                                            (ByVal ZipCode As String, _
                                                                            ByVal szKen As String, _
                                                                            ByVal szCty1 As String, _
                                                                            ByVal szCty2 As String, _
                                                                            ByVal szTwn As String, _
                                                                            ByVal szTwnExt As String) As Long

Public Sub ConvZip2arrAddr(ByRef zipCd As String, arrAddr() As String)
    
    Dim pref    As String * 40
    Dim city1   As String * 40
    Dim city2   As String * 40
    Dim town1   As String * 40
    Dim town2   As String * 500
    Dim arrRet(4)    As String
    
    On Error GoTo ErrFunc
    
    If zipCd = vbNullString Then Exit Sub
    If Len(zipCd) <> 7 Then Exit Sub

    If Val(zipCd) Then
        zcGetZipDecision zipCd, pref, city1, city2, town1, town2
        arrRet(0) = Left$(pref, InStr(pref, vbNullChar) - 1)
        arrRet(1) = Left$(city1, InStr(city1, vbNullChar) - 1)
        arrRet(2) = Left$(city2, InStr(city2, vbNullChar) - 1)
        arrRet(3) = Left$(town1, InStr(town1, vbNullChar) - 1)
        arrRet(4) = Left$(town2, InStr(town2, vbNullChar) - 1)
        ReDim arrAddr(0 To 2)
        arrAddr(0) = arrRet(0)
        arrAddr(1) = arrRet(1) & arrRet(2)
        arrAddr(2) = arrRet(3) & arrRet(4)
    End If
    Exit Sub
    
ErrFunc:
    Debug.Print "No." & Err.Number & ":" & Err.Description
End Sub

Access2019で試したが、他のバージョンでもそれほど違いはないと思う。

Add a Comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください