473,387 Members | 1,611 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.

SQL in VBA + iterating through recordset

Hi NG!

I just managed copying data from one table to another table (boah,
what a big deal :). But there are still some problems:

1)
I want to use the keyword "BETWEEN" in combination with date fields in
my SELECT statement, but Execute does not like it. I

tried different variations, none of them works :(

My code: (IFrom and ITo are input fiels with format date, short)

sInsSQL = "INSERT INTO _VATAP_temp (REC, PVM, AIKA, TAPLAJI,
TUOTE, VARASTO ) "
sSelSQL = "SELECT REC, PVM, AIKA, TAPLAJI, TUOTE, VARASTO FROM
_VATAP_temp2 "

'does not work
'sSelSQL = sSelSQL & "WHERE (PVM Between Forms!abc_1!IFrom And
Forms!abc_1!ITo)"

'also not this
'sSelSQL = sSelSQL & "WHERE (PVM Between #" & IFrom & "# And #"
'sSelSQL = sSelSQL & ITo & "#)"

'and also not this alternative solution...
'sSelSQL = sSelSQL & "WHERE (PVM >= " & IFrom & " And PVM <="
'sSelSQL = sSelSQL & ITo & ")"
sInsSQL = sInsSQL & sSelSQL

'always stops with runtime error 3075, syntax error
dbs.Execute sInsSQL

I noticed that the query would require the date in the format
#12/07/80#. Is there a conventient way of doing that?
2)
In the next step I have to iterate through all results, make some
calculation and write the result to another column in this

table.

For Each In Table._VATAP_temp
// well, the next line is C++-style, but I hope you know what I mean
with that...
currentRow.setField("result", currentRow.getField("data") * 1,22)
End For
As you can see I am new in VBA, so I would really appreciate to give
me some links for good VBA-tutorials.
Thank you in advance,
Rhanda
Nov 12 '05 #1
2 7960
On 25 Feb 2004 13:29:01 -0800, gh*****@gmx.net (Rhanda Zak) wrote:
Hi NG!

I just managed copying data from one table to another table (boah,
what a big deal :). But there are still some problems:

1)
I want to use the keyword "BETWEEN" in combination with date fields in
my SELECT statement, but Execute does not like it. I

tried different variations, none of them works :(

My code: (IFrom and ITo are input fiels with format date, short)

sInsSQL = "INSERT INTO _VATAP_temp (REC, PVM, AIKA, TAPLAJI,
TUOTE, VARASTO ) "
sSelSQL = "SELECT REC, PVM, AIKA, TAPLAJI, TUOTE, VARASTO FROM
_VATAP_temp2 "

'does not work
'sSelSQL = sSelSQL & "WHERE (PVM Between Forms!abc_1!IFrom And
Forms!abc_1!ITo)"
Right. If you're using DAO/ADO to execute the query, the query does not "see"
Access obects, so you have to either make a querydef that takes parameters or
insert the data into the SQL as you are trying to do.

'also not this
'sSelSQL = sSelSQL & "WHERE (PVM Between #" & IFrom & "# And #"
'sSelSQL = sSelSQL & ITo & "#)"

'and also not this alternative solution...
'sSelSQL = sSelSQL & "WHERE (PVM >= " & IFrom & " And PVM <="
'sSelSQL = sSelSQL & ITo & ")"

These should work, and I can't tell you exactly why they don't. By the way,
it's probably better to say Me!IFrom instead of IFrom. The next thing to try
is to print out sInsSQL before executing it to make sure it looks like we
expect it ti.

sInsSQL = sInsSQL & sSelSQL

'always stops with runtime error 3075, syntax error
dbs.Execute sInsSQL

I noticed that the query would require the date in the format
#12/07/80#. Is there a conventient way of doing that?
Actually, it will work with any valid date format, but it's best to make it an
unambiguous format with respect to internationalization. That would be ...#"
& Format(Me!IFrom,"yyyy-mm-dd") & "#...
2)
In the next step I have to iterate through all results, make some
calculation and write the result to another column in this

table.

For Each In Table._VATAP_temp
// well, the next line is C++-style, but I hope you know what I mean
with that...
currentRow.setField("result", currentRow.getField("data") * 1,22)
End For
As you can see I am new in VBA, so I would really appreciate to give
me some links for good VBA-tutorials.


1. For Each requires a variable between the "Each" and the "In"
2. A recordset is not a collection, so you can't iterate it using For Each
anyway. Instead, in DAO, you first need an If block to skip the loop entirely
unless .RecordCount <> 0 (.RecordCount may not be accurate, but it works and
is correct for this purpose), then loop While Not rst.EOF, and do rst.Movenext
just before the Wend statement at the end of the loop.
3. A tabledef is not a recordset. I contains information about the table's
definition, not its contents. You must open a recordset based on the table
name or a Select statement to loop through a table's rows.
Nov 12 '05 #2
"Rhanda Zak" <gh*****@gmx.net> wrote in message
news:16*************************@posting.google.co m...

<snip>
'also not this
'sSelSQL = sSelSQL & "WHERE (PVM Between #" & IFrom & "# And #"
'sSelSQL = sSelSQL & ITo & "#)"


try formatting the date in Amercian format...

'sSelSQL = sSelSQL & "WHERE (PVM Between #" & Format(IFrom,
"mm/dd/yyyy") & "# And #"
'sSelSQL = sSelSQL & Format(ITo, "mm/dd/yyyy") & "#)"
<snip>
--
Bradley
Software Developer www.hrsystems.com.au
A Christian Response www.pastornet.net.au/response
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a...
2
by: nettid1 | last post by:
I'm a little new to Access so please bear with me... I have a query in Access consisting of three fields: Account Number, Query ID and Description. I want to have a button on a form that calls...
2
by: rcoutts | last post by:
I have a table with no ID field. I would like to read a single field of each record using DLookup or something similar and pass it to a VB function that's already been written. However, DLookup...
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
14
by: Jacko | last post by:
Hi guys, Say I made a SELECT statement to my sql DB that would return 50 rows that I will use a sqldatareader to access. Instead of iterating through each and every row of the datareader, I'd...
5
by: steph | last post by:
Hi, I'm a bit of an access-dummy, and i suppose this problem of mine has a rather simple solution, but nevertheless i'm not sure i see it at the moment. I've got an access-form in...
3
by: mark blackall | last post by:
Hi all, I am new to vb.net and I am trying to use the vb.net components, rather than relying on the VB6 compatibility stuff with which I am more familiar. However, I seem to have fallen at...
5
by: akselo | last post by:
Hi there good folks, I am trying to populate a column in a table with a number incrementing for each class (as set by another field; 176 different classes in table of 150,000 records representing...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.