dbTalk Databases Forums  

SQL In VBA

comp.databases.ms-access comp.databases.ms-access


Discuss SQL In VBA in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Barry
 
Posts: n/a

Default SQL In VBA - 04-03-2011 , 10:49 AM






On a form I want to on load create in SQL records to be added to a
table. The records consist of todays date so that is a variant in the
sql and then there isa loop that goes 7 times. I end up with 7 records
showing blank date.
Code below

Private Sub Form_Load()
Dim dbs As Database
Dim Datenow As Date

Dim count As Integer
Set dbs = CurrentDb

Datenow = Date - 1
count = 0
Do Until count = 7
Datenow = Datenow + 1

' sql = "INSERT INTO Datestoselect (dates) " & " Values (" &
Datenow & ");"
Dates = Datenow
dbs.Execute ("INSERT INTO Datestoselect (dates) " & " Values (" &
Datenow & ");")

count = count + 1
Loop

End Sub

Any ideas ?

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: SQL In VBA - 04-03-2011 , 11:44 AM






Barry wrote:
Quote:
On a form I want to on load create in SQL records to be added to a
table. The records consist of todays date so that is a variant in the
"variable", not "variant"

Quote:
sql and then there isa loop that goes 7 times. I end up with 7 records
showing blank date.
Code below

Private Sub Form_Load()
Dim dbs As Database
Dim Datenow As Date

Dim count As Integer
Set dbs = CurrentDb

Datenow = Date - 1
count = 0
Do Until count = 7
Datenow = Datenow + 1

' sql = "INSERT INTO Datestoselect (dates) " & " Values (" &
Datenow & ");"
Dates = Datenow
dbs.Execute ("INSERT INTO Datestoselect (dates) " & " Values (" &
Datenow & ");")

count = count + 1
Loop

End Sub

Any ideas ?
About what? What is the question? Oh wait. Is the question "why am I getting
blank dates"?

I do see that yu define a variable (sql) that you don't subsequently use
(don't worry about appending the semicolons - they aren't needed). Why is
that? Assigning the sql to a variable is a great idea because it allows you
to inspect it while steppping through the code. Only you should then execute
it like this:

dbs,Execute sql

What's supposed to be the purpose of this line: "Dates = Datenow"? It seems
to have no purpose at all.

I suggest putting a breakpoint in that code and stepping through it (this is
a great time to learn how to debug).

If I was doing this task, I would not be using a loop. I would use a utility
table called Numbers, a table with a single column called Num containg
numbers 0-1000 (1001 rows).. This, in addition to many other uses, would
allow me to do this:

INSERT DateToSelect (dates) SELECT Date() + Num FROM Numbers where Num
between 0 and 6

You could either save this in a query to be executed in VBA or assign the
statement to a string variable to be executed. No loop needed.

Reply With Quote
  #3  
Old   
Marshall Barton
 
Posts: n/a

Default Re: SQL In VBA - 04-03-2011 , 01:15 PM



Barry wrote:

Quote:
On a form I want to on load create in SQL records to be added to a
table. The records consist of todays date so that is a variant in the
sql and then there isa loop that goes 7 times. I end up with 7 records
showing blank date.
Code below

Private Sub Form_Load()
Dim dbs As Database
Dim Datenow As Date

Dim count As Integer
Set dbs = CurrentDb

Datenow = Date - 1
count = 0
Do Until count = 7
Datenow = Datenow + 1

' sql = "INSERT INTO Datestoselect (dates) " & " Values (" &
Datenow & ");"
Dates = Datenow
dbs.Execute ("INSERT INTO Datestoselect (dates) " & " Values (" &
Datenow & ");")

count = count + 1
Loop

End Sub

I think Bob's use of the Numbers table is clearly the best
way to do that.

But to try to answer your specific question, the problem I
see is that the date value ends up being an expression when
the query is executed:
. . . Values (4/3/2010)
and 4 divided by 3 divided by 2010 evaluates to a pretty
small number, i.e. just a few seconds.

To concatenate a date into your SQL you need the result to
be a date literal. I would do it this way:

sql = "INSERT INTO Datestoselect (dates) Values (" _
& Format(Datenow, "\#yyyy-m-d\#") & ")"

--
Marsh

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.