Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    taylorosso is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2009
    Posts
    29

    small if statement help please

    i have a list box which i populate via a search box.

    When I enter my search string the list box shows all of the matching results,
    the user then clicks 'ok' and the selected record is loaded into a form.

    This works great untill it finds no matches. If i click "ok" the form then crashes.

    I would like to create an IF statemnt for my "ok" button that says:


    If List returns no results (i.e is empty) then

    Close form

    else......(runs my other code)



    Hope you can help

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The form should not crash regardless of what you are doing. What are you doing after the user presses "OK"?

  3. #3
    taylorosso is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2009
    Posts
    29
    This is the dialogue i get when I select 'ok' ;

    syntax error (missing operator) in query expression '[clientID]='


    Basicly the 'ok' button opens the 'Clients' form and displays the selected record from the list box, then closes the search form.

    Ive attached a gif for you to help picture whats going on.

    cheers

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I don't see an "OK" button. Is there code behind the button? What makes the button appear? Is it a Click or DoubleClick event on the ListBox? If so then what is the code in that event?

  5. #5
    taylorosso is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2009
    Posts
    29
    my bad its the "continue" button im referring to.

    here is the code behind the button:
    Private Sub Command3_Click()

    On Error GoTo Err_Command3_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "frmClients"

    stLinkCriteria = "[ClientID]=" & Me!
    [List3]
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    DoCmd.Close acForm, "frmSearchClient2", acSaveNo

    DoCmd.Close acForm, "frmSearchClient1", acSaveNo

    Exit_Command3_Click:
    Exit Sub

    Err_Command3_Click:
    MsgBox Err.Description
    Resume Exit_Command3_Click


    End Sub
    here is the code behind the form;
    Private Sub Form_Load()
    Dim strSQL As String
    Dim MyFilter As String
    If Len(Me.Filter) > 0 Then
    MyFilter = "WHERE " & Me.Filter
    Else
    MyFilter = ""
    End If
    strSQL = "SELECT Clients.ClientID, " & _
    "Clients.Firstname, " & _
    "Clients.Surname " & _
    "FROM Clients " & _
    MyFilter & ";"
    Me.List3.RowSource = strSQL
    End Sub

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Now I think you have posted enough details to offer a solution if I could figure out what form you want to close in your first post.

  7. #7
    taylorosso is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2009
    Posts
    29
    the form i will be closing is the form in the gif. (frmSearchClient2).

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    So you want to close the form with the button on it which you will do in any case right? The OnLoad code you posted is from the frmClients form right?

  9. #9
    taylorosso is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2009
    Posts
    29
    the onload code is from frmSearchClient2,

    That search list box is actually another form that opens to sit infront of the parent form which is 'Clients'.

    The 'Continue' button then closes this 'frmSearchClient2' to reveal the parent form sitting beneath

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You managed to confuse me with your last post so I'll just take a flyer. Try:
    Code:
    Private Sub Command3_Click()
       On Error GoTo Err_Command3_Click
       Dim stDocName As String
       Dim stLinkCriteria As String
       If Me.List3.ListCount > 0 Then
          stDocName = "frmClients"
          stLinkCriteria = "[ClientID]=" & Me![List3]
          DoCmd.OpenForm stDocName, , , stLinkCriteria
       End If
       DoCmd.Close acForm, "frmSearchClient2", acSaveNo
       DoCmd.Close acForm, "frmSearchClient1", acSaveNo
    Exit_Command3_Click:
       Exit Sub
    Err_Command3_Click:
       MsgBox Err.Description
       Resume Exit_Command3_Click
    
    End Sub

  11. #11
    taylorosso is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2009
    Posts
    29
    ha sorry mate,

    it all makes so much sense when i am writing it out :P

    I'll give your solution a try and get back to you.

    Cheers

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Describing what the code does wrong may help me better understand what you need.

  13. #13
    taylorosso is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2009
    Posts
    29
    I used your if statement in the code for my command button.

    Now when there are no search results and the user presses 'continue' it just closes the search box instead of try to open the next form with a selected record.

    My working code is as follows

    Private Sub Command3_Click()

    On Error GoTo Err_Command3_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    If Me.List3.ListCount > 0 Then
    stDocName = "frmClients"
    stLinkCriteria = "[ClientID]=" & Me!
    [List3]
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    DoCmd.Close acForm, "frmSearchClient2", acSaveNo
    DoCmd.Close acForm, "frmSearchClient1", acSaveNo
    Else
    DoCmd.Close acForm, "frmSearchClient2", acSaveNo
    DoCmd.Close acForm, "frmSearchClient1", acSaveNo
    End If

    Exit_Command3_Click:
    Exit Sub

    Err_Command3_Click:
    MsgBox Err.Description
    Resume Exit_Command3_Click

    End Sub
    Thanks for the help again RuralGuy, I knew what i wanted to do just didnt know how to phrase my syntax.

  14. #14
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Didn't the code I posted do *exactly* the same thing as the code you are using?

  15. #15
    taylorosso is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2009
    Posts
    29
    yeah it did, I just needed the If statement part really

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need someone to develope a small database program for me
    By Cleaner in forum Database Design
    Replies: 4
    Last Post: 10-05-2009, 07:21 AM
  2. IIF statement
    By james1982 in forum Access
    Replies: 1
    Last Post: 07-20-2009, 09:38 AM
  3. Best way to organize a small database of people?
    By Orabidoo in forum Database Design
    Replies: 1
    Last Post: 06-09-2009, 10:13 PM
  4. how to use IF then statement
    By ronnie4 in forum Access
    Replies: 1
    Last Post: 03-17-2009, 11:32 PM
  5. multiple iif statement NEED HELP PLZ
    By scott munkirs in forum Reports
    Replies: 1
    Last Post: 09-27-2006, 05:21 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