Solved

Access Form: Using an apostrophe?

Posted on 2012-04-06
8
249 Views
Last Modified: 2012-06-21
Good Afternoon!

I am working on a form in Access for my employees to use to create weekly task reports.

There is a field for "Tasks."  Once this is filled out, the user clicks on a button and the task is added to two tables and I am able to create a report.

I am running into the problem that the user receives an error message if an apostrophe is used. I have seen code that allows a particular string to have an apostrophe, but I don't know what to do if the apostrophe could be entered at any time in different descriptions of the tasks.

How do I fix this?

Thank you!
0
Comment
Question by:Megin
  • 4
  • 3
8 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37817410
two ways you can treat this problem, assuming string is varString

1. use chr(34) as wrapper of the string

     chr(34) & varString & chr(34)

2. use the replace function    > replace 1 ' single quote with 2 '  single quotes
    '" & replace(varString,"'", "''") & "'  
     '" & replace(varString,chr(39),chr(39) & chr(39)) & "'
0
 
LVL 75
ID: 37817412
Surround with double quote = Chr(34)

Chr(34) & YourStringWithApostrophe & Chr(34)

mx
0
 

Author Comment

by:Megin
ID: 37817617
I am so sorry, but I am new to this. Where do I put this code?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37817673
where are you using the string with apostrophes'?

give more detailed explnation
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Megin
ID: 37817687
The field is "ActName."

The code is below.




Private Sub btnAdd_Click()
DoCmd.SetWarnings False

Dim strSQL As String
Dim db As Database
Dim rst As DAO.Recordset
Dim LngItem As Long

Set db = CurrentDb()




Dim rowc As Integer


With Me.LstNewAct
  For rowc = 0 To .ListCount - 1
    strSQL = "SELECT * FROM tbl_Activities WHERE actName='" & .Column(0, rowc) & "'"
    
    Set rst = db.OpenRecordset(strSQL)
    If (rst.BOF And rst.EOF) Then ' There are no records if Beginning-Of-File and End-Of-File are both true.
      DoCmd.RunSQL "INSERT INTO tbl_Activities (actName) VALUES ('" & .Column(0, rowc) & "')"
      rst.Close
    End If
    strSQL = "SELECT actID FROM tbl_Activities WHERE actName='" & .Column(0, rowc) & "'"
    Set rst = db.OpenRecordset(strSQL)
    DoCmd.RunSQL "INSERT INTO tbl_ActCmb1 (toID, stoID, actID, actDate, actType) VALUES (cmbTO, cmbsto, '" & rst.Fields(0).Value & "', WkDate, actType1)"
  Next rowc
End With



With Me.LstAct
  For Each varItem In .ItemsSelected
    LngItem = .Column(0, varItem)
    strSQL = "INSERT INTO tbl_ActCmb1 (toID, stoID, actID, actDate, actType) VALUES (cmbTO, cmbsto, " & LngItem & ", WkDate, actType1)"
    DoCmd.RunSQL strSQL, dbFailOnError
    
  Next varItem
  
End With
DoCmd.SetWarnings True

Open in new window

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37817694
try this format

strSQL = "SELECT * FROM tbl_Activities WHERE actName=" & chr(34) &  .Column(0, rowc) & chr(34)
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 37817698
Private Sub btnAdd_Click()
DoCmd.SetWarnings False

Dim strSQL As String
Dim db As Database
Dim rst As DAO.Recordset
Dim LngItem As Long

Set db = CurrentDb()




Dim rowc As Integer


With Me.LstNewAct
  For rowc = 0 To .ListCount - 1
    strSQL = "SELECT * FROM tbl_Activities WHERE actName=" & chr(34) &  .Column(0, rowc) & chr(34)
    
    Set rst = db.OpenRecordset(strSQL)
    If (rst.BOF And rst.EOF) Then ' There are no records if Beginning-Of-File and End-Of-File are both true.
      DoCmd.RunSQL "INSERT INTO tbl_Activities (actName) VALUES (" & chr(34) & .Column(0, rowc) & chr(34) & ")"
      rst.Close
    End If
    strSQL = "SELECT actID FROM tbl_Activities WHERE actName=" & chr(34) &  .Column(0, rowc) & chr(34) & "
    Set rst = db.OpenRecordset(strSQL)
    DoCmd.RunSQL "INSERT INTO tbl_ActCmb1 (toID, stoID, actID, actDate, actType) VALUES (cmbTO, cmbsto, '" & rst.Fields(0).Value & "', WkDate, actType1)"
  Next rowc
End With



With Me.LstAct
  For Each varItem In .ItemsSelected
    LngItem = .Column(0, varItem)
    strSQL = "INSERT INTO tbl_ActCmb1 (toID, stoID, actID, actDate, actType) VALUES (cmbTO, cmbsto, " & LngItem & ", WkDate, actType1)"
    DoCmd.RunSQL strSQL, dbFailOnError
    
  Next varItem
  
End With
DoCmd.SetWarnings True
                                            

Open in new window

0
 

Author Closing Comment

by:Megin
ID: 37817703
Thank you! That worked like a charm!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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.

760 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

21 Experts available now in Live!

Get 1:1 Help Now