Pull details through from r.Harrison amazing postcode lookup sample (1 Viewer)

FAB1

Registered User.
Local time
Today, 13:31
Joined
Jul 27, 2007
Messages
40
Hi

After finding r.Harrison amazing postcode lookup example in the Sample databases section i imported the frm into my db. On my frm_NewClient added a button to open the postcode lookup frm. I am now stuck with the code for splitting up the address into my fields and adding them to my frm_NewClient

Could anybody offer some code examples that I could add to the listBox OnClick event

I think the code would have to cover
1. Select the required address
2. Copy it
3. Paste/Update my frm_NewClient fields HouseName, HouseNo, Street, TownCity, County, PostCode
4. Close the postcode lookup frm

Thanking you in advance for your help in this matter
Stewart

r.Harrison Code

Option Compare Database

'Requires reference to WinHTTP.dll This can be found in 'C:\Windows\System32\'

Private Sub Text0_AfterUpdate()

Me.List2.RowSource = ""
Dim Pcode, sStr As String
'First up, Format the postcode
Pcode = UCase(Replace(Text0, " ", ""))
Select Case Len(Pcode)
Case 5
Pcode = Mid(Pcode, 1, 2) & " " & Mid(Pcode, 3, 3)
Case 6
Pcode = Mid(Pcode, 1, 3) & " " & Mid(Pcode, 4, 3)
Case 7
Pcode = Mid(Pcode, 1, 4) & " " & Mid(Pcode, 5, 3)
End Select

'Now create the search string
'Had to delete as u.r.l used and this only my 4th post
sStr = "Had to delete as u.r.l used and this only my 4th post
For a = 1 To Len(Pcode)
If IsNumeric(Mid(Pcode, a, 1)) Then
sStr = sStr & Mid(Pcode, 1, a - 1) & "/"
a = Len(Pcode)
End If
Next a
sStr = sStr & Mid(Replace(Pcode, " ", "-"), 1, InStr(Pcode, " ") + 1) & "/"
sStr = sStr & Replace(Pcode, " ", "-") & "/"

'Now I create a WinHTTP request to get the information from the server

Dim winReq As WinHttpRequest
Dim HTM, Address As Variant
Dim Add1, Add2, Add3, Add4 As String
Dim sCount As Integer

Set winReq = New WinHttpRequest
With winReq
.Open "GET", sStr, False
.Send
HTM = Split(Replace(.ResponseText, """", "'"), "<")
If .Status <> 200 Then
MsgBox ("Address not found")
Exit Sub
End If
End With

'Now I have the entire web page including tags just without the '<' at the beginning of each line
'Split this down to find the address lines
For Each i In HTM
If InStr(i, "td class='address'>") > 0 Then
'You can the assign the address to a listbox as below
Me.List2.AddItem (Replace(i, "td class='address'>", ""))

'Or you can split the address in to variables
Address = Split((Replace(i, "td class='address'>", "")), ",")
sCount = 0
For Each j In Address
sCount = sCount + 1
Next
Select Case sCount
Case 3
Add1 = Address(0)
Add4 = Address(1)
Case 4
Add1 = Address(0)
Add3 = Address(1)
Add4 = Address(2)
Case 5
Add1 = Address(0)
Add2 = Address(1)
Add3 = Address(2)
Add4 = Address(3)
Case 6
Add1 = Address(0) & " " & Address(1)
Add2 = Address(2)
Add3 = Address(3)
Add4 = Address(4)
End Select
'Put code here to assign these variables to anything you like

End If
Next

End Sub
 

pr2-eugin

Super Moderator
Local time
Today, 13:31
Joined
Nov 30, 2011
Messages
8,494
Stewart, I use the same PostCode lookup Form on my application..

I use the listbox double click event.. Also I normally pass the name of the Form that calls the Post Code lookup Form, so that I will be able to assign them back.. Something like..
Code:
Private Sub List2_DblClick(Cancel As Integer)
    If Len(Me.OpenArgs & vbNullString) > 0 Then
            Forms([COLOR=Green]Me.OpenArgs[/COLOR])![COLOR=Blue]add1TxtBox[/COLOR] = Me.List2.Column(0, List2.ItemsSelected)
            Forms([COLOR=Green]Me.OpenArgs[/COLOR])![COLOR=Blue]add2TxtBox[/COLOR] = Me.List2.Column(1, List2.ItemsSelected) & " " & List2.Column(2, List2.ItemsSelected)
            Forms([COLOR=Green]Me.OpenArgs[/COLOR])![COLOR=Blue]add3TxtBox[/COLOR] = Me.List2.Column(3, List2.ItemsSelected)
            Forms([COLOR=Green]Me.OpenArgs[/COLOR])![COLOR=Blue]countyTxtBox[/COLOR] = Me.List2.Column(4, List2.ItemsSelected)
            Forms([COLOR=Green]Me.OpenArgs[/COLOR])![COLOR=Blue]PostCodeTxtBox[/COLOR] = UCase(Me.Text0.Value)
    End If
    DoCmd.Close acForm, "frm_Lookup"
End Sub
As you can see that I have used the OpenArgs to get the value for which the data needs to be assigned, I also have a very generic Form setup where most of my Form design will have a common name like addnTxtBox. This way it made the usage of this postcode lookup form, across many different forms..

So when calling the lookup form I use..
Code:
DoCmd.OpenForm "frm_Lookup", _
            OpenArgs:="WillsDetailsForm"
 

FAB1

Registered User.
Local time
Today, 13:31
Joined
Jul 27, 2007
Messages
40
Hi Paul, Thanks for the code i will have a go
Cheers
Stewart
 

Users who are viewing this thread

Top Bottom