Sql Syntax (1 Viewer)

ianclegg

Registered User.
Local time
Today, 01:46
Joined
Jul 14, 2001
Messages
58
Hi

I am trying to extract records from a table using variables gained from another table, unfortunately I cannot create the correct syntax. I have 3 variables,i have shown my sql below

Sql = "select seqno,surname,forename,diedon,warcemetery from ByCemetery where surname = CWGC_surname"

If I have only 1 variable the code works but if I have more than 1 it does not, I need 3, the Surname, the forename and the diedon fields, this will enable me to extract the warcemetery field from the other table.

Any help would be much appreciated.

Regards

Ian Clegg
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 20:46
Joined
Jan 23, 2006
Messages
15,378
Ian,

Because of the quotes, your sql is being treated as a
literal. Try this
Sql = "select seqno,surname,forename,diedon,warcemetery from ByCemetery where surname = '" & CWGC_surname &"'"
 

isladogs

MVP / VIP
Local time
Today, 01:46
Joined
Jan 14, 2017
Messages
18,216
Try this. I've added text delimiters to the WHERE clause:

Code:
Sql = "SELECT ByCemetery.seqno, ByCemetery.surname, ByCemetery.forename, ByCemetery.diedon, ByCemetery.warcemetery FROM ByCemetery WHERE surname = '" & CWGC_surname & "'"

However, you haven't said where you are trying to use this

SELECT sql statements can't be run using VBA
However its fine for a select query, record source for a form or row source for a combobox / listbox

EDIT jdraw is well named as quicker on the draw :D
 

ianclegg

Registered User.
Local time
Today, 01:46
Joined
Jul 14, 2001
Messages
58
Thanks for your help, I have fixed my mistake, a different approach always helps the cause.

Once again Thanks

Regards

Ian Clegg
 

Mark_

Longboard on the internet
Local time
Yesterday, 17:46
Joined
Sep 12, 2017
Messages
2,111
Ian,

Something that may help you quickly identify these kinds of errors;
When I want to pass an SQL statement to anything, I first put the entire text into a variable. This allows me to display it in one of several ways, thus quickly identifying when I'm doing something wrong. For your case, the code would look something like

Code:
DIM asSQL as string
asSQL = "SELECT seqno,surname,forename,diedon,warcemetery FROM ByCemetery WHERE surname = '" & CWGC_surname& "'"
MsgBox "SQL is " & asSQL

Often reviewing what you will do prior to using SQL means you can quickly spot problems in your SQL statement.
 

Users who are viewing this thread

Top Bottom