Results 1 to 4 of 4
  1. #1
    virgilio is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2019
    Posts
    81

    How to pass and use primary key as openargs?

    Can I use the autonumber ID which is my primary key to open a form based on a record?



    I have a new database set up to:
    1. select a record from a continuous form
    2. click a button (called "Open Material Record") to open a form based on that record
    3. pass the variable as an open argument
    4. use the open argument to set a query string for the recordsource


    The number passed as my open argument is correct, but when I do (step 4) I get a type error: "run-time error '3464': Data type mismatch in criteria expression"

    Here's my code:

    On the Open Material Record button:

    Code:
    Private Sub Mat_Open_Button_Click()   Dim idNumber As String
       idNumber = Me!ID_NUMBER  ' this is a control on the continuous form that displays the ID 
       'DoCmd.Close acForm, "Landing_Page"
       DoCmd.OpenForm "RAM_Form", OpenArgs:=idNumber
    End Sub
    On the edit record form:
    Code:
    Private Sub Form_Open(Cancel As Integer)
      
       If Nz(Me.OpenArgs) = 0 Then
          Me.RecordSource = "WASTE_INVENTORY"
          DoCmd.RunCommand acCmdRecordsGoToNew
       Else
          Me.RecordSource = "Select WASTE_INVENTORY.* FROM WASTE_INVENTORY WHERE (((WASTE_INVENTORY.ID)='" & Me.OpenArgs & "'));"
       End If
    
    end sub
    It is not pictured above, but I have tried setting a variable to me.openargs and then type converting to variant or integer using CVar and CInt, but they are just the wrong variable types.

    I would normally use a unique identifier as a SK, but the field that I would use for that is blank on two records from the import. I guess I could just put a number in there, but I'd like to make it work this way if possible.

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Try:
    Code:
    Me.RecordSource = "Select WASTE_INVENTORY.* FROM WASTE_INVENTORY WHERE (((WASTE_INVENTORY.ID)=" & Me.OpenArgs & "));"
    I've taken out the single quotations marks. They are only used as delimiters around text values. Not needed for numbers.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    virgilio is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2019
    Posts
    81
    Yep, that worked. Thanks!

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Quote Originally Posted by virgilio View Post
    Yep, that worked. Thanks!
    You're welcome. Glad to help if I can
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Replies: 7
    Last Post: 03-11-2015, 12:48 PM
  2. Replies: 4
    Last Post: 04-01-2014, 02:33 PM
  3. Replies: 5
    Last Post: 01-27-2013, 06:04 PM
  4. How to set openargs of sub report?
    By flippedbeyond in forum Programming
    Replies: 11
    Last Post: 09-20-2011, 08:24 PM
  5. Replies: 8
    Last Post: 08-03-2011, 09:09 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