-1

I am working on pulling information from a table into a Check In & Out form. We will be cataloging helmets using barcodes. With the way I have the form set up, scanning a barcode will automatically display the information for that helmet. Each helmet will then be timestamped when it is shipped out and again when it is returned. Below is the code I have so far.

Private Sub Search_Button_Click()
    
    Dim rst As DAO.Recordset
    Dim strsql As String
    
    strsql = "Select\* From Helmets Where UPC Code = " & _
             txt.ScanUPCCode.Value & ""
    
    Set rst = CurrentDb.OpenRecordset(strsql)
    
    If rst.EOF Then
        MsgBox "No data found"
        txt.ScanUPCCode.Value = Nothing
        txt.HelmetID = Nothing
        txt.School = Nothing
    Else
        txt.HelmetID.Vaule = rst.Fields("HelmetIDNumber")
        txt.School.Vaule = rst.Fields("School")
    End If
    
    rst.Close
    
    Set rst = Nothing
    
End Sub

New contributor
Tiffani-Michelle Schmidt is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
6
  • 2
    What is your question? What is not working as expected? Commented Nov 25 at 21:36
  • 2
    Before posting code, at least make sure it compiles - for example txt.HelmetID.Vaule is clearly a typo. After you've fixed those issues, run your code and tell us exactly what the error is, and on which line of your code. FYI you do not clear a text box by assigning Nothing to it - that is for object references. Likely you want something like txt.School = "" Commented Nov 25 at 21:53
  • Also if the UPC value is not numeric then you need quotes - strsql = "Select * From Helmets Where UPC Code = '" & txt.ScanUPCCode.Value & "'" Commented Nov 25 at 21:54
  • Value is default property for data controls, don't even need to type it - as you did not for 2 controls. Be consistent. Aside from typos already mentioned, why is there a backslash in the SQL string? Commented Nov 25 at 22:47
  • And what is txt and why is it followed by a period? Commented Nov 26 at 3:46

1 Answer 1

1

This is very much along the lines of checking OUT/IN book at library. Might want to review MS Lending Library template.

Since HelmetID, UPC, School are associated data, really should not be saving UPC and School into OUT/IN log, just HelmetID (unless this association is subject to change and you need to save history). Scan UPC into a combobox and most (maybe all) of this code can be eliminated. Combobox would have a RowSource that includes HelmetID, UPC, School fields. UPC would display but value would be ID.

If you want to stick with posted code, considering two assumptions:

  1. txt is a prefix for textbox names not followed by a period
  2. UPC code field is a text data type

clean up code like:

Private Sub Search_Button_Click()
    
Dim rst As DAO.Recordset
Dim strsql As String

With Me    
    strsql = "Select * From Helmets Where UPC Code = '" & _
             .txtScanUPCCode.Value & "'"
    
    Set rst = CurrentDb.OpenRecordset(strsql)
    
    If rst.EOF Then
        MsgBox "No data found"
        .txtScanUPCCode.Value = Null
        .txtHelmetID.Value = Null
        .txtSchool.Value = Null
    Else
        .txtHelmetID.Value = rst.Fields("HelmetIDNumber")
        .txtSchool.Value = rst.Fields("School")
    End If
    
    rst.Close
    
    Set rst = Nothing
End With    
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.