Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    May 2002
    Location
    Roanoke, Virginia, USA
    Posts
    493
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Apostrophe Problem (A2K SR1)

    I have a tblCustomer which stores Name, Address etc about a customer. I have a
    frmCustomerEntry which is used to input the customer data. I have a Before Update event on the
    form as follows:

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    'The BeforeUpdate event allows you to test to see
    'if a record exists before inserting it a second time.
    If Me.NewRecord = True Then
    Dim strWhere As String, strMessage As String
    strWhere = "LastName = '" & Me!LastName & "'"
    Me.RecordsetClone.MoveFirst
    Me.RecordsetClone.FindFirst strWhere
    Do Until Me.RecordsetClone.NoMatch
    If Me.RecordsetClone!FirstName = Me!FirstName Then
    strMessage = "This guest is already in the database. "
    strMessage = strMessage & " Please verify the information being entered."
    strMessage = strMessage & " Click OK and the original record will be displayed."
    MsgBox strMessage, vbInformation, "Matching Record"
    Cancel = True
    DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo
    Me.Bookmark = Me.RecordsetClone.Bookmark
    Exit Sub
    End If
    Me.RecordsetClone.FindNext strWhere
    Loop
    End If
    End Sub

    My problem is when a name is inputted that contains an apostrophe, we get a Run-time error

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Apostrophe Problem (A2K SR1)

    You probably won't encounter names that contain a double quote, so you could try putting double quotes around the name instead of single quotes. One way to do that is to use Chr(34):<pre>strWhere = "LastName = " & Chr(34) & Me!LastName & Chr(34)</pre>

    or you could use double double quotes inside a string:<pre>strWhere = "LastName = """ & Me!LastName & """"</pre>

    but personally, I find this proliferation of quotes confusing - that is a quadruple double quote at the end!

  3. #3
    4 Star Lounger
    Join Date
    May 2002
    Location
    Roanoke, Virginia, USA
    Posts
    493
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Apostrophe Problem (A2K SR1)

    I agree that the Chr (34) is much more readable.

    Many thanks for the solution.

    Tom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •