Solved

Access data with apostrophe and pass through query

Posted on 2013-05-15
4
690 Views
Last Modified: 2013-05-15
I have a form that collects parameters from users to perform a search (frmSearchEstToEdit)

After filling in their criteria, it brings up another form with the results (frmSearchEstToEditResults).  This form also passes the data through to a pass-through query to execute a stored procedure to query the tables in the onOpen event.

If the user inputs any data with an apostrophe, then it obviously wont work in the pass through query.

I adjusted my stored procedure so that when it runs, it does not take into account any apostrophes.  Then, on the Access end I tried to remedy this by stripping the apostrophe before sending it to the pass through query.  I did this by creating a hidden text box on the input form that uses the replace function and simply takes out the apostrophes.  Doing this solves the problem on that field, but then creates a "data type mismatch" error if the user enters any data in any of the other fields on the input form.  So, I tried solving it by using the replace function in the code.  Again, it works beautifully on the field I was trying to fix, but then it creates a "invalid null" error if the user tries to put any data in any of the other fields on the form.  Both these errors appear to be occurring on the Access form and not even making it to the pass-through query, so I think it is an issue in my code.

Any ideas on how I can strip the apostrophe so that this will work without getting errors elsewhere?

This is the string I am passing through to the pass-through query from frmSearchEstToEditResults...

Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize
    Dim dbs As Database
    Dim qdf As QueryDef
    Dim frm As Form

    Set dbs = CurrentDb
    Set frm = Forms!frmSearchEstToEdit
    Set qdf = dbs.QueryDefs("qrySQLSearchEstToEdit")
    Dim rstForm As DAO.Recordset
qdf.SQL = "EXEC spSearchEstToEdit " & "'" & Forms!frmSearchEstToEdit!txtFacilityNumber & "'" & "," & "'" & Forms!frmSearchEstToEdit!txtFacilityName & "'" & "," & "'" & Forms!frmSearchEstToEdit!OwnerName & "'" & "," & "'" & Forms!frmSearchEstToEdit!txtFacilityAddress & "'" & "," & "'" & Forms!frmSearchEstToEdit!txtFacilityCity & "'" & "," & "'" & Forms!frmSearchEstToEdit!txtFacilityZip & "'" & "," & "'" & Forms!frmSearchEstToEdit!txtFacilityPIN & "'"

qdf.ReturnsRecords = True
Me.Requery
Set rstForm = qdf.OpenRecordset()
If rstForm.EOF Then
   MsgBox "There are no records to display.  Please change your search criteria."
    DoCmd.CancelEvent
    DoCmd.OpenForm "frmSearchEstToEdit"
 Else
rstForm.MoveFirst
Set qdf = Nothing
Set frm = Nothing
Me.Refresh
End If
End Sub
0
Comment
Question by:dcgimo
  • 2
  • 2
4 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39169188
you need to double the quotes:

qdf.SQL = "EXEC spSearchEstToEdit " & "'" & replace(Forms!frmSearchEstToEdit!txtFacilityNumber, "'", "''") & "','" & replace(Forms!frmSearchEstToEdit!txtFacilityName, "'", "''") & "','" & replace(Forms!frmSearchEstToEdit!OwnerName, "'", "''") & "','" & replace(Forms!frmSearchEstToEdit!txtFacilityAddress, "'", "''") & "','" & replace(Forms!frmSearchEstToEdit!txtFacilityCity, "'", "''") & "','" & replace(Forms!frmSearchEstToEdit!txtFacilityZip, "'", "''") & "','" & replace(Forms!frmSearchEstToEdit!txtFacilityPIN, "'", "''") & "'"

BTW, the & "'" & "," & "'" & can be reduced to & "','" &
0
 

Author Comment

by:dcgimo
ID: 39169215
Thanks for the response!  

I did try the double quotes w/replace but then I get the "invalid use of null" error.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 39169313
do you which field is null?

you can try to concatenate an empty string to each value:

replace(Forms!frmSearchEstToEdit!txtFacilityNumber & "", "'", "''")
0
 

Author Comment

by:dcgimo
ID: 39169413
That resolved it.  Thank you so much for your help!!!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question