[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Access Form: Using an apostrophe?

Posted on 2012-04-06
8
Medium Priority
?
322 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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.
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.

656 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