Solved

Access Form: Using an apostrophe?

Posted on 2012-04-06
8
267 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

861 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

22 Experts available now in Live!

Get 1:1 Help Now