Results 1 to 12 of 12
  1. #1
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172

    Dim As String syntax help

    Hi, I have this code which is working perfectly:



    Private Sub cmdbutton1_Click()
    Dim sWHERE As String

    sWHERE = "[MyQuery].[MyField] = '" & Me.cboField & "'"


    'Open the form
    DoCmd.OpenForm "MyForm", acNormal, , sWHERE


    End Sub

    I got this basic syntax on-line, however, I want to add an AND clause, and just don't understand the use of " or ' and so I can't get it working. I've tried a number of different variations. Basically I want to have something like this:


    Private Sub cmdbutton1_Click()
    Dim sWHERE As String

    sWHERE = "[MyQuery].[MyField1] = '" & Me.cboField1 & "'"

    AND "[MyQuery].[MyField2] = '" & Me.cboField2 & "'"

    'Open the form
    DoCmd.OpenForm "MyForm", acNormal, , sWHERE


    End Sub

    Any help would be appreciated.

    Thanks,
    Kirsti

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You almost had it.
    Code:
    Private Sub cmdbutton1_Click()
    Dim sWHERE As String
    
    sWHERE = "[MyQuery].[MyField1] = '" & Me.cboField1 & "' AND [MyQuery].[MyField2] = '" & Me.cboField2 & "'"
    
    '------ comment out (or delete) the next line after debugging
    Debug.Print swhere
    
    'Open the form
    DoCmd.OpenForm "MyForm", acNormal, , sWHERE
    
    End Sub
    You use the single quotes (known as delimiters) if the data in cboField1 and cboField2 are text.
    If "MyField1" was a date type, you would use "#" as the delimiters.
    Numbers do not require delimiters.

  3. #3
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Thanks Steve...

    The strange thing is that part of the code is working ok, but I am now getting Run-time error '2501': The OpenForm action was canceled.

    Kirsti

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    but I am now getting Run-time error '2501': The OpenForm action was canceled.
    When are you getting the error?
    Before the form opens or after the form opens?

  5. #5
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Hi there,

    I'm getting the error before the form opens.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try
    Code:
    Private Sub cmdbutton1_Click()
    Dim sWHERE As String
    
    sWHERE = "[MyField1] = '" & Me.cboField1 & "' AND [MyField2] = '" & Me.cboField2 & "'"
    
    '------ comment out (or delete) the next line after debugging
    Debug.Print swhere
    
    'Open the form
    DoCmd.OpenForm "MyForm", acNormal, , sWHERE
    
    End Sub
    Note the blue line

    What is returned from the debug line?

    Will the form open or is there an error if you use:

    Code:
    DoCmd.OpenForm "MyForm", acNormal

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    If you need a visual on what Steve is suggesting:

    BaldyWeb-Immediate window

    I visited NZ a couple of months ago; my daughter moved there in January. Hope you didn't get shaken up too much last week!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    I definitely have the blue line right.

    The line returned from the debug line is:

    DoCmd.OpenForm "MyForm", acNormal, , sWHERE

    And yes, it opens using:
    DoCmd.OpenForm "MyForm", acNormal

  9. #9
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Thanks Paul, I will take a look at your video.

    I live in Christchurch, so am used to a bit of shaking, but as luck would happen, happened to be on holiday right near Seddon last week! All ok though.

  10. #10
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Thank you thank you thank you.

    Immediate Window helped.

    It was a data mismatch error - and I now have it sorted.

    Thank you both again,

    Kirsti

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Oh, yes you've had some bad ones recently. My daughter lives in Wellington, but luckily had no significant damage. I hope to get to the South Island on my next visit. We only made it up as far as Martinborough and Castle Point. Spent most of the trip right around Wellington.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    sWHERE = "[MyField1] = '" & Me.cboField1 & "' AND [MyField2] = '" & Me.cboField2 & "'"
      '------ comment out (or delete) the next line after debugging 
    Debug.Print swhere
    Should have been something like
    "[MyField1] = 'Hello' and [MyField2] = 'Goodby'"

    Are both fields text type?

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

Similar Threads

  1. String
    By Rhubie in forum Queries
    Replies: 1
    Last Post: 05-28-2013, 09:15 PM
  2. MySQL Syntax Error from Query String in VBA
    By raynman1972 in forum Programming
    Replies: 2
    Last Post: 09-17-2012, 05:59 PM
  3. Vba string
    By rchtan in forum Programming
    Replies: 5
    Last Post: 05-17-2012, 04:29 AM
  4. SQL + VBA String
    By jgelpi16 in forum Programming
    Replies: 2
    Last Post: 08-22-2011, 08:44 AM
  5. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 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