Results 1 to 8 of 8
  1. #1
    bell is offline Advanced Beginner
    Windows 7 64bit Access 2002
    Join Date
    Feb 2015
    Posts
    37

    How to open a record that has two keys, one is a Text and the other is a Number

    Hi all,



    This is probably related to my last question...
    I had a database working with two keys, both numbers (Part Number and Serial Number), but I had to change Part Number to Text to allow for dashes

    I was using the following code to find a specific record (it finds the last Serial Number I have in the database for a particular Part Number) but my code now does work when I have the Part Number as a Text (I get the error: Data type mismatch in criteria expression)

    Code:
    Private Sub Last_Record_Click()On Error GoTo Err_Last_Record_Click
    
    
        Dim criteria As String
        Dim lastSerial As Integer
        
        lastSerial = Me![Serial Number]
        
        criteria = "[Part Number]=" & Me![Part Number] & " AND [Serial Number]=" & lastSerial
        
        Do While (DCount("*", "Route Table", [critieria]) = 1)
            lastSerial = lastSerial + 1
            criteria = "[Part Number]=" & Me![Part Number] & " AND [Serial Number]=" & lastSerial
        Loop
        
        lastSerial = lastSerial - 1
        criteria = "[Part Number]=" & Me![Part Number] & " AND [Serial Number]=" & lastSerial
        Me.Form.Recordset.FindFirst [criteria]
        Me.[Operation Form].Form.Recordset.FindFirst [critieria]
        Me.SetFocus
    
    
    Exit_Last_Record_Click:
        Exit Sub
    
    
    Err_Last_Record_Click:
        MsgBox Err.Description
        Resume Exit_Last_Record_Click
    In my last question, the mod posted:
    Parameters for text field need apostrophe delimiters, date fields use #, numbers don't have delimiters.

    So I am assuming the problem is because my Part Number is a Text and my Serial Number is a Number

    I tried doing

    criteria = "[Part Number] = ' " & Me![Part Number] & " ' " & " AND [Serial Number]=" & lastSerial

    but that also doesn't work

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    criteria = "[Part Number] = '" & Me![Part Number] & "' AND [Serial Number]=" & lastSerial
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Edit: I was looking at the wrong variable, nevermind

    The following is only good for numbers -32,000 to 32,000 (give or take several hundred)
    Dim lastSerial As Integer

    So maybe
    Dim lastSerial As String

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    1st yes, data type error means youre trying to put a string in a number field.

    2nd Dont use FINDFIRST , just open a query , say qsFindData. in it is your where clause.
    select * from [Route Table] where [Part Number]=" & Me![Part Number] & " AND [Serial Number]=" & lastSerial

    IF you have lastSerial defined as number in the table, and part = text in the table.

  5. #5
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    try removing the me!

    if you want to use me, it should be me.

    also, tho' not sure if it is relevant

    Me.[Operation Form].Form.Recordset.FindFirst [critieria]

    has a typo - and you shouldn't need the square brackets

  6. #6
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you cant use ME in a query. you must use the entire path of forms..
    forms!frmMyForm!partnum

  7. #7
    bell is offline Advanced Beginner
    Windows 7 64bit Access 2002
    Join Date
    Feb 2015
    Posts
    37
    Quote Originally Posted by ranman256 View Post
    you cant use ME in a query. you must use the entire path of forms..
    forms!frmMyForm!partnum
    Thanks for the reply

    I am close to getting the solution I want

    I got the code down to just this

    Code:
    DoCmd.OpenForm "Route and Rework Form", , , "[Part Number]='" & Forms![Route and Rework Form]![Part Number] & "'" & " AND [Serial Number]= (SELECT Max([Serial Number]) FROM [Route Table] WHERE [Part Number]='" & Forms![Route and Rework Form]![Part Number] & "')"
    So if I have the form "Route and Rework Form" opened on a particular Part Number and Serial Number, and I click a this button, it will open up the form to the last Serial Number for that given Part Number...
    Which is good, but I wanted it to just bring focus to the last Serial Number, but still have the other records with the same Part Number still opened, so I could potentially switch back to them...

    How can I do this?

  8. #8
    bell is offline Advanced Beginner
    Windows 7 64bit Access 2002
    Join Date
    Feb 2015
    Posts
    37
    Never mind, I ended up doing this

    Code:
    lastSerial = DMax("[Serial Number]", "[Route Table]", "[Part Number]='" & Forms![Route and Rework Form]![Part Number] & "'")
        Call DoCmd.GoToRecord(acDataForm, "Route and Rework Form", acGoTo, lastSerial)
    This finds the max serial number for that part number, and opens the form to that record

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

Similar Threads

  1. Replies: 5
    Last Post: 11-30-2014, 12:46 PM
  2. Replies: 3
    Last Post: 06-17-2012, 11:55 AM
  3. Replies: 1
    Last Post: 02-17-2012, 10:17 AM
  4. Replies: 3
    Last Post: 10-23-2009, 05:03 PM
  5. Are text primary keys less efficient than autonumbers?
    By bar tomas in forum Database Design
    Replies: 4
    Last Post: 05-11-2009, 09:37 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