Solved

Need help formatting a string from a text box

Posted on 2011-09-08
7
187 Views
Last Modified: 2012-05-12
I have a form which displays the name of the Holiday and I wish to use that name to update a field in another field in another table.  However the way that I have written my code it errors out on the fact that there are spaces in the name of the Holiday, such as "Washingons Birthday" or "Fourth of July".  It would not be a problem if it stripped out the spaces and inserted the name without the spaces but I am not sure of the function to call to do that.  I am using Access 2007 in an ADP format and this is only part of the query.  I have been able to get it functional to this point.

Where it is failing is when I reference Left([Forms]![Holiday]![Holiday],25)
strSQL = " Update AdjustedSchedule"
    strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST =" & [Forms]![HOLIDAY]![DOW] & ","
    strSQL = strSQL & " AdjustedSchedule.HOLIDAY =" & Left([Forms]![HOLIDAY]![HOLIDAY], 25) & ","
    strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"
    DoCmd.RunSQL (strSQL)

Open in new window

0
Comment
Question by:JasBrad
  • 3
  • 2
  • 2
7 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 36505243
You need to enclose the values in single quotes, just as you have for the STATUS field.
0
 
LVL 33

Accepted Solution

by:
Norie earned 65 total points
ID: 36505256
Oops, forgot the code.

strSQL = " Update AdjustedSchedule"    
strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST ='" & [Forms]![HOLIDAY]![DOW] & "',"    
strSQL = strSQL & " AdjustedSchedule.HOLIDAY ='" & Left([Forms]![HOLIDAY]![HOLIDAY], 25) & "',"   
strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"    DoCmd.RunSQL (strSQL)

Open in new window

0
 
LVL 9

Assisted Solution

by:borki
borki earned 60 total points
ID: 36505265
I think you are missing the string delimiters. Try

strSQL = " Update AdjustedSchedule"
    strSQL = strSQL & " Set AdjustedSchedule.HOLADJUST =" & [Forms]![HOLIDAY]![DOW] & ","
    strSQL = strSQL & " AdjustedSchedule.HOLIDAY = '" & Left([Forms]![HOLIDAY]![HOLIDAY], 25) & "',"
    strSQL = strSQL & " AdjustedSchedule.STATUS = 'Holiday';"
    DoCmd.RunSQL (strSQL)

0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:JasBrad
ID: 36505373
That worked but one other question.  How do I get it to ignore apostrophes in the name? or is that not possible?
0
 
LVL 9

Expert Comment

by:borki
ID: 36505446
You would need to parse the string before hand and replace any instance of a single apostrophe with two apostrophes, eg

replace(string, "'", "''")

or

Left(Replace([Forms]![HOLIDAY]![HOLIDAY], "'", "''"), 25)

Very hard to read ;-)
0
 
LVL 33

Expert Comment

by:Norie
ID: 36505507
borki

You can always use Chr(39).

Replace(string, Chr(39), Chr(39) & Chr(39))

Open in new window

Or, if you are using it a lot declare a constant.
Const SQ = "'"  ' Single Quote

Open in new window

Still quite messy though.:)
0
 

Author Comment

by:JasBrad
ID: 36505577
Thanks!!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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.

770 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