Results 1 to 7 of 7
  1. #1
    bcournoyer is offline Novice
    Windows 10 Office 365
    Join Date
    Oct 2023
    Posts
    9

    Passing a Field Value to a Variable

    Beginners question. I'm just learning VBA for Access and I'm having an issue setting a field value to a variable. See code below.



    I can get the value of the field I click on (ORDERNO) and even display it in a msgbox. But I cannot get it assigned to a variable. When I run the below code I get an Object Require error highlighting the Set orderNum line.
    I've tried the following for that line.

    Code:
    Set orderNum = myF![ORDERNO].Value
    and
    Set orderNum = Str(myF![ORDERNO])
    What's the obvious error that my inexperience cannot see?

    Thx

    Code:
    Private Sub ORDERNO_Click()
        Dim myF As Form, orderNum As String, whereCnd As String
        Set myF = Forms("frmSalesOrderList")
        'MsgBox myF![ORDERNO]
        Set orderNum = myF![ORDERNO]
        Set whereCnd = "[ORDERNO] = " & orderNum
        MsgBox whereCnd
        'docmd.OpenForm(frmSalesOrderInfo,acNormal,,whereCnd,,,)
    End Sub

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Why bother with the variable. You can reference [ORDERNO] in Docmd.OpenForm() method.
    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
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'm with Bob but the error is because you don't want the "Set" keyword. Should be:

    orderNum = myF![ORDERNO].Value
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    variables can be set with simple equal:
    dim sMyVar as string
    sMyVar = me.txtBox


    OBJECTS are set using SET:
    dim obj as object
    set obj = txtBox

    now obj is now a text box obj.
    not needed just to grab the value.

  5. #5
    bcournoyer is offline Novice
    Windows 10 Office 365
    Join Date
    Oct 2023
    Posts
    9
    Thanks guys, I now understand the Set better. I also saw as Bob said my error was in the openForm method. Replaced all my code with this:

    Code:
    Private Sub ORDERNO_Click()
        DoCmd.OpenForm "frmSalesOrderInfo", acNormal, , "[ORDERNO] = '" & orderNum & "'"
    End Sub

  6. #6
    bcournoyer is offline Novice
    Windows 10 Office 365
    Join Date
    Oct 2023
    Posts
    9
    Oops,

    Correct code:

    Code:
    Private Sub ORDERNO_Click()
        Dim myF As Form
        Set myF = Forms("frmSalesOrderList")
        DoCmd.OpenForm "frmSalesOrderInfo", acNormal, , "[ORDERNO] = '" & myF![ORDERNO] & "'"
    End Sub

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Declaring then setting the form variable there does not make sense - you don't use it.
    Also perhaps worth mentioning that when you Set anything, it's generally considered good practice to reclaim that memory space by setting it to Nothing.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. passing a control name as as variable
    By newbieX in forum Programming
    Replies: 3
    Last Post: 09-26-2014, 02:06 PM
  2. Passing variable in DLookup (VBA)
    By rustynails in forum Programming
    Replies: 2
    Last Post: 08-22-2013, 10:43 AM
  3. Passing a variable back from a sub
    By RonL in forum Programming
    Replies: 7
    Last Post: 01-25-2013, 12:10 AM
  4. Passing SQL result into variable
    By jonny in forum Access
    Replies: 3
    Last Post: 10-18-2009, 07:46 AM
  5. Passing a variable to a form
    By cjamps in forum Forms
    Replies: 0
    Last Post: 03-02-2009, 05:32 AM

Tags for this Thread

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