Solved

Access data with apostrophe and pass through query

Posted on 2013-05-15
4
648 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 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
Thanks for the response!  

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

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
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
Comment Utility
That resolved it.  Thank you so much for your help!!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now