Results 1 to 3 of 3
  1. #1
    sjs94704 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Location
    Berkeley, CA
    Posts
    20

    Making a Public Function more flexible

    In my database I have a table of 42,000 ZIP codes with City and State included. Basically, the idea is that once the user selects a ZIP code that the database populates the City and State text boxes with the matching information.

    As you can see in the following function, the form name Forms!CarRentalContractF is hard coded into this function.

    My desired result is to change this form name into a variable of some kind so that I can use this function anywhere in the database I might need it. Please provide your instructions with your suggestions on how I can alter this function to accomplish my goal.


    Public Function DisplayCity()

    Dim db As Database
    Dim rs As Recordset
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * from ZIPCodeQ WHERE ZIPCodeID=" & Forms!CarRentalContractF!ZIPCodeCombo)
    Forms!CarRentalContractF!State = rs!StateAbreviation
    Forms!CarRentalContractF!City = rs!City
    rs.Close
    db.Close
    Set rs = Nothing
    Set db = Nothing


    End Function

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Perhaps this suggestion from MVP Allen Browne will be useful: http://allenbrowne.com/ser-16.html

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Since you are writing code, you should learn about passing arguments (variables) to procedures.

    http://msdn.microsoft.com/en-us/libr...=vs.60%29.aspx
    http://www.cpearson.com/excel/byrefbyval.aspx



    Here is the subroutine I came up with:
    Code:
    Public Sub GetCityState(pstrForm As Form, pZip As Long)
       Dim db As DAO.Database
       Dim rs As DAO.Recordset
       Dim frm As Form
    
       Set db = CurrentDb
       Set frm = pstrForm
    
       Set rs = db.OpenRecordset("SELECT City, StateAbreviation from ZIPCodeQ WHERE ZIPCodeID=" & pZip)
       If (rs.BOF And rs.EOF) Then
          MsgBox "Zip code - " & pZip & " not found"
       Else
          frm!StateAbreviation = rs!StateAbreviation
          frm!City = rs!City
       End If
    
       rs.Close
       db.Close
       Set rs = Nothing
       Set db = Nothing
       Set frm = Nothing
    
    End Sub
    You call it like this:
    Code:
    Private Sub Combo3_AfterUpdate()
       Call GetCityState(Me, Combo3)
    End Sub

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

Similar Threads

  1. Make column name flexible
    By dani9 in forum Queries
    Replies: 6
    Last Post: 09-29-2011, 01:13 AM
  2. public variables
    By zul in forum Programming
    Replies: 3
    Last Post: 08-23-2011, 11:11 AM
  3. Search By Criteria - Flexible Criteria Fields
    By lilanngel in forum Access
    Replies: 0
    Last Post: 03-16-2011, 06:25 PM
  4. Replies: 9
    Last Post: 12-20-2010, 08:05 PM
  5. public instead of dim not working
    By DKY in forum Access
    Replies: 1
    Last Post: 10-14-2008, 11:42 AM

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