473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pound signs in field names causing problems in VBA; how do I get around this?

I am trying to create a passthrough query in Access using VBA code. I have done this several times before successfully, but in this instance, I have run up against a wall due to a field name that contains a pound sign.
Below is the VBA code that I am using:

Private Sub Command0_Click()

DoCmd.DeleteObject acQuery, "MyQ2"

Dim MyDb As DAO.Database

Dim MyQ2 As QueryDef

Dim sSQL2 As String

sSQL2 = "SELECT I.""ACT#"", I.TRTE, C.NME1, C.NME2, C.ADRS, C.CYST, C.ZP, C.OGDT, C.OGAM, C.CPFG FROM MTGBPN.INTRN I LEFT JOIN MTGBP1.CHTR# C ON I.ACT# = C.ACT# WHERE I.TRTE BETWEEN" & " " & Me.Date1 & " " & "AND" & " " & Me.Date2 & " " & "Order By I.""ACT#"""

Set MyDb = CurrentDb()

Set MyQ2 = MyDb.CreateQueryDef("MyQ2")
MyQ2.SQL = sSQL2

MyQ2.ReturnsRecords = True
MyQ2.Connect = "ODBC;DSN=HALS"

End Sub

Running this code produces the following VBA error: Invalid use of '.', '!', or '()' in query expression 'I."ACT#"=C."ACT#'.
If I remove the delaminating quotations from the field names in the SQL string JOIN statement, I get the following VBA error: Syntax error (missing operator) in query expression 'I.ACT#=C.ACT'.
(Note: If I remove the quotations and enclose the field names in the JOIN statement in square brackets: ON I.[ACT#]=C.[ACT], the code executes and the passthrough is created. However, the passthrough itself will not execute due to the square brackets in the SQL statement.)
I realize that this is a very nice example of why field names should not be created that contain such special characters. I wish I could change the field names but I cannot; I have read-only access to the data.
I would be extremely grateful if someone could show a way to write the string so that it is acceptable to VBA, or some sort of what around this issue.
Thanks very much in advance!
P.S. The dates in the SQL string are dates in name only, they are in number format, Example
Jan 13 '11 #1
1 5143
Stewart Ross
2,545 Expert Mod 2GB
Hi. Problem is that the pound sign is a reserved character in Access used in pairs as a date literal delimiter, like this:

WHERE SomeDate > #1/1/2011#

The Access query engine, not VBA, sees it as a syntax error to use the pound sign as a single character following a field name. This is true for both types of recordset which you can use in Access - its native DAO and the more generic ADODB types.

The only way to get Access to accept the field name is to delimit the field name in brackets, but as you have found this precludes the use of a pass-through query in your environment.

I'm sorry to say that I can think of no obvious solution to this problem at present.

-Stewart
Jan 13 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Keith Wilby | last post by:
I have a form which allows both form and datasheet views. My question is, is it possible to control the field (column) names in datasheet view? What happens at the moment is that the name is...
1
by: Pathfinder | last post by:
Hi I have a question for you... what if I have a table with different field names as example Field1,Field2,Field3,Field4 and Field5. I want to make only one query where I can run it and based on...
2
by: Ralph | last post by:
I'm trying to import a range of cells from an Excel spreadsheet into a table in access. The first row contains column labels, but I cannot use those as my field names, both because of their format...
0
by: Michel Rousseau | last post by:
I have a table created by a query. I would like to create a query which would create the exact same table but rename some of the field names. How could I do this? Thanks. Michel Rousseau
3
by: bitoulis | last post by:
Hi, is it possible to use the records of a table as the field names of another table? If yes, how is it done? Thanks in advance Laertes
0
by: tshad | last post by:
I have been trying to set up some services that keep conflicting with each other because of VS2003 default names. Even though I set up a service, such as EmailService, it still sets up the file...
1
by: Mark Rae | last post by:
Hi, Suddenly (at least, I think so) when I rebuild a web deployment project in VS.NET 2005, it strips off British pound signs e.g. £10 becomes 10. Has anyone else seen this...? Is there a way...
11
by: Tim Hunter | last post by:
Hi I am using WinXP and Access 2003 Is it possible to store the field names of a table in an array and then loop through the array and update the table using the field names stored in the array? I...
4
by: Bob | last post by:
Hi all, I've got a table that I've imported and it has junk at the top of the table, so after import I run a delete query to remove the junk lines then I'm left with the field names I want for...
0
by: RobR2009 | last post by:
I am having trouble with a C# proxy page I am writing which allows me to do cross domain AJAX calls with Javascript. The problem is with certain pages that contain pound signs £ that are not HTML...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.