Solved

Access Form: Using an apostrophe?

Posted on 2012-04-06
8
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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
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)

 
LVL 120

Expert Comment

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

give more detailed explnation
0
 

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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37817694
try this format

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

Accepted Solution

by:
Rey Obrero (Capricorn1) 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

733 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