Results 1 to 2 of 2
  1. #1
    wnicole is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    118

    Calculate distance using fields in form

    I have this code below that was given to me. However
    Instead of entering the postal code continually or selecting from the combo box. How do I set zipcode 1 and zipcode 2 based on fields in a form. I would like to use the current FROM (Default zip based on the current job in form) and TO zipcode [Project Postal Code] that is on the form at that time. Not necessarily combo box.

    Currently you have
    zip1 = Me.Combo2.Value
    zip2 = Me.Combo4.Value

    I want Zip 1 to be Default zip (however I can still change it if needed)
    ZipCode field is [Project Postal Code]

    Can you help me? Not that savvy with replacing. code.
    Thanks
    Here is the full code:
    Private Sub Command6_Click()
    Dim zip1 As String, zip2 As String
    Dim sResponse As String
    Dim sLink As String
    On Error GoTo Command6_Click_Error

    zip1 = Me.Combo2.Value
    zip2 = Me.Combo4.Value
    Dim sHold As String
    sHold = GetDistanceBetweenTwoZips(zip1, zip2)
    MsgBox "The distance between " & zip1 & " and " & zip2 & " is " & Mid(sHold, 1, InStr(sHold, "|") - 1) _
    & vbCrLf & " The estimated time to get there is " & Mid(sHold, InStr(sHold, "|") + 1)

    sResponse = MsgBox("Do you want to see a map of the route?", vbYesNo, "Want a Map")
    If sResponse = vbYes Then
    sLink = "http://maps.google.com/maps?f=q&hl=en&q="
    Application.FollowHyperlink sLink & zip1 & " to " & zip2


    End If

    On Error GoTo 0
    Exit Sub

    Command6_Click_Error:
    If Err.Number = 94 Then
    MsgBox "You must make zip selections before clicking the button"
    Exit Sub
    Else
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Command6_Click of VBA Document Form_frmZipSample"
    End If
    End Sub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    The simple answer is to place the following into the Form's load or current event.

    Me.Combo2.Value = Me.Field1.Value
    Me.Combo4.Value = Me.Field2.Value

    The complex answer involves how your data is structured and the use of tables that store default setting options.

    I don't understand where the proposed default value resides. I would advise against hardcoding the value into VBA. Also, because you have a combo box, passing a prime key or foreign key value to the combo box is preferred. I believe there are limitations to the data type you can pass to a combo box. I always use a PK. You may be able to get around this by adjusting the bound column property of the combo box, I don't know.

    Hope this helps.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to calculate fields in 2 tables in VBA
    By dluhut in forum Access
    Replies: 2
    Last Post: 04-23-2013, 05:08 PM
  2. Replies: 5
    Last Post: 03-06-2013, 02:31 PM
  3. Replies: 5
    Last Post: 02-11-2013, 08:34 AM
  4. Replies: 34
    Last Post: 12-01-2011, 08:18 AM
  5. Replies: 1
    Last Post: 12-12-2010, 05:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums