Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2007
    Posts
    3

    Checking a record in a table

    I was hoping someone could help me here.



    I've got a form that a user enters a map number into. I want to check a master list of map numbers (stored in a table) to see if it exists. I'd like something of a true/false answer so I can then direct the user to the next appropriate form. Right now I just send the user to a form and then refresh the list boxes on the form based on the number provided. If the map number is new or wrong it just shows an empty form. I'd rather have something that checks the table ahead of time and pops up a message box if no record exists. Here is my current code:

    Private Sub Command0_Click()
    On Error GoTo Err_Command0_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim frm As Form, txt As TextBox
    Dim frm2 As Form, txt2 As TextBox, txt3 As TextBox

    stDocName = "search_records"
    stLinkCriteria = "[MAP_NUMB]='" & Me.strMap & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Set frm = Forms![search_records]
    Set txt = Forms![search_records]!Text1
    frm.RefreshLists
    frm.SetFocus
    txt.SetFocus
    End If

    Exit_Command0_Click:
    Exit Sub

    Err_Command0_Click:
    MsgBox Err.description
    Resume Exit_Command0_Click

  2. #2
    jya is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2007
    Location
    Chicagoland
    Posts
    109

    Something to Try...

    Would adding what I put in bold help? If the recordset is empty, close the form and give a message that there was no match.

    Private Sub Command0_Click()
    On Error GoTo Err_Command0_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim frm As Form, txt As TextBox
    Dim frm2 As Form, txt2 As TextBox, txt3 As TextBox

    stDocName = "search_records"
    stLinkCriteria = "[MAP_NUMB]='" & Me.strMap & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    If Forms(stDocName).Recordset.EOF then
    DoCmd.Close acform, stDocName
    msgbox "No map was found"
    Else


    Set frm = Forms![search_records]
    Set txt = Forms![search_records]!Text1
    frm.RefreshLists
    frm.SetFocus
    txt.SetFocus
    End If

    Exit_Command0_Click:
    Exit Sub

    Err_Command0_Click:
    MsgBox Err.description
    Resume Exit_Command0_Click

  3. #3
    Join Date
    Nov 2007
    Posts
    3
    Thank you. That works good. I think I'd rather not have the other screen flash open and then close though.

    Anyone else have any ideas?

  4. #4
    jya is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2007
    Location
    Chicagoland
    Posts
    109

    Alternate Option

    Hadn't used this code recently, but it will run a query instead of opening a form and see if there is a match.

    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    Dim strSQL As String

    strSQL = "Select * from search_records where [Map_numb] = '" & Me!strMap & "'"

    rs.ActiveConnection = CurrentProject.Connection
    rs.CursorType = adOpenDynamic
    rs.LockType = adLockOptimistic
    rs.Open strSQL

    If rs.EOF Then
    MsgBox "no record was found"
    Else


    To All,

    Another place to go for Access help would be

    http://www.access-programmers.co.uk/forums/

  5. #5
    Join Date
    Nov 2007
    Posts
    3
    thanks for the help. that works perfectly.

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

Similar Threads

  1. Replies: 2
    Last Post: 06-06-2011, 04:18 PM
  2. Replies: 5
    Last Post: 03-23-2011, 10:39 AM
  3. Replies: 0
    Last Post: 05-16-2008, 07:50 AM
  4. Need help checking database design
    By abc7 in forum Database Design
    Replies: 1
    Last Post: 10-29-2007, 08:08 AM
  5. Replies: 0
    Last Post: 06-30-2006, 09:00 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