dbTalk Databases Forums  

Pass a Combo Box value to an Insert Query

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


Discuss Pass a Combo Box value to an Insert Query in the comp.databases.ms-access forum.



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

Default Pass a Combo Box value to an Insert Query - 01-20-2012 , 03:26 PM






Hi,

I have a combo box cboDate with the value "01/01/12" on a form. I
click a button on the same form to run a query to insert records into
a table. I want to replace the #01/01/12# value in vSQL with the date
value in cboDate? How do I do this?

Button Code:

Private Sub cmdBuildMembersTemp_Click()

'Append all the records in qryTemp5 to tblMembersTemp

Dim qry As QueryDef
Dim vSQL As String

'First Delete all records in tblMembersTemp
Set qry = CurrentDb.CreateQueryDef("", "DELETE * FROM
tblMembersTemp")
qry.Execute

vSQL = "INSERT INTO tblMembersTemp ( lngMembershipNumber,
CurrentDate, FirstName, LastName " & _
"SELECT qryTemp5.lngMembershipNumber,
Format(#01/01/12#,""mmmm d, yyyy"") AS CurrentDate,
qryTemp5.FirstName, qryTemp5.LastName " & _
"FROM qryTemp5;"

Set qry = CurrentDb.CreateQueryDef("", vSQL)
qry.Execute
qry.Close
Set qry = Nothing

end Sub


Thanks, appreciate your help.

Barry

Reply With Quote
  #2  
Old   
XPS350
 
Posts: n/a

Default Re: Pass a Combo Box value to an Insert Query - 01-20-2012 , 04:29 PM






"SELECT qryTemp5.lngMembershipNumber, Format(#" & cboDate & "#,"'mmmm d, yyyy") ÁS CurrentDate, qryTemp5.FirstName, qryTemp5.LastName " & _
"FROM qryTemp5;"

Peter

Reply With Quote
  #3  
Old   
Barry Edmund Wright
 
Posts: n/a

Default Re: Pass a Combo Box value to an Insert Query - 01-20-2012 , 05:01 PM



Sorry it did not work. The exact expression I used was:
(#"" & cboDate & ""#,""mmmm d, yyyy"")
In order to get a single quote you must put two in. Anyway the exact
error was:
Syntax error in date in query expression 'Format(#" & cboDate & "#,
"mmmn d, yyyy")'.

On Jan 20, 5:29*pm, XPS350 <xps... (AT) gmail (DOT) com> wrote:
Quote:
"SELECT qryTemp5.lngMembershipNumber, Format(#" & cboDate & "#,"'mmmm d, yyyy") ÁS CurrentDate, qryTemp5.FirstName, qryTemp5.LastName " & _
"FROM qryTemp5;"

Peter

Reply With Quote
  #4  
Old   
Bob Quintal
 
Posts: n/a

Default Re: Pass a Combo Box value to an Insert Query - 01-20-2012 , 05:28 PM



Barry Edmund Wright <barry.edmund.wright (AT) witstoronto (DOT) ca> wrote in
news:72fa5219-e831-4746-abde-af0ae17e1eca (AT) m11g2000yqe (DOT) googlegroups.co
m:

Quote:
Hi,

I have a combo box cboDate with the value "01/01/12" on a form. I
click a button on the same form to run a query to insert records
into a table. I want to replace the #01/01/12# value in vSQL with
the date value in cboDate? How do I do this?

Button Code:

Private Sub cmdBuildMembersTemp_Click()

'Append all the records in qryTemp5 to tblMembersTemp

Dim qry As QueryDef
Dim vSQL As String

'First Delete all records in tblMembersTemp
Set qry = CurrentDb.CreateQueryDef("", "DELETE * FROM
tblMembersTemp")
qry.Execute

vSQL = "INSERT INTO tblMembersTemp ( lngMembershipNumber,
CurrentDate, FirstName, LastName " & _
"SELECT qryTemp5.lngMembershipNumber,
Format(#01/01/12#,""mmmm d, yyyy"") AS CurrentDate,
qryTemp5.FirstName, qryTemp5.LastName " & _
"FROM qryTemp5;"

Set qry = CurrentDb.CreateQueryDef("", vSQL)
qry.Execute
qry.Close
Set qry = Nothing

end Sub


Thanks, appreciate your help.

Barry
The table stores the date as a double precision number, with the
whole portion representing the number of days since 1899/12/31 (e.g.
Jan 1 1900 is day 1) and the fractional part gives the time (noon is
..5, 6pm is .75). Therefore, attempting to format the date will cause
problems.

You also need to make vba evaluate the contents and write the result
to the string,

Try
vSQL = "INSERT INTO tblMembersTemp " & _
"(lngMembershipNumber, CurrentDate, FirstName, LastName) " & _
"SELECT qryTemp5.lngMembershipNumber, " & _
"#" & me.cbodate & "# AS CurrentDate, " & _
"qryTemp5.FirstName, qryTemp5.LastName " & _
FROM qryTemp5;"



--
Bob Q.
PA is y I've altered my address.

Reply With Quote
  #5  
Old   
Douglas J Steele
 
Posts: n/a

Default Re: Pass a Combo Box value to an Insert Query - 01-21-2012 , 05:40 AM



You don't need the # characters around the combo box reference:

'Format(" & cboDate & ",""mmmn d, yyyy"")

"Barry Edmund Wright" wrote in message
news:a4254eb7-4d3c-48d0-9b29-863d716e3b02 (AT) e27g2000vbu (DOT) googlegroups.com...

Sorry it did not work. The exact expression I used was:
(#"" & cboDate & ""#,""mmmm d, yyyy"")
In order to get a single quote you must put two in. Anyway the exact
error was:
Syntax error in date in query expression 'Format(#" & cboDate & "#,
"mmmn d, yyyy")'.

On Jan 20, 5:29 pm, XPS350 <xps... (AT) gmail (DOT) com> wrote:
Quote:
"SELECT qryTemp5.lngMembershipNumber, Format(#" & cboDate & "#,"'mmmm d,
yyyy") ÁS CurrentDate, qryTemp5.FirstName, qryTemp5.LastName " & _
"FROM qryTemp5;"

Peter

Reply With Quote
  #6  
Old   
Barry Edmund Wright
 
Posts: n/a

Default Re: Pass a Combo Box value to an Insert Query - 01-21-2012 , 03:18 PM



Thanks, I realized from your advice and Doug's code that the vSQL
wanted a string so I created a variable vDate as string and populated
it with the string value of the formated date in the combo box then
used that in the vSQL.

The code below worked - don't forget the single quote on both sides of
the vdate:
'" & vDate & "'

Dim vDate As String

vDate = CStr(Format(CDate(Me.cboDate), "mmmm d, yyyy"))

* vSQL = "INSERT INTO tblMembersTemp " & _
* "(lngMembershipNumber, CurrentDate, FirstName, LastName) " & _
* "SELECT qryTemp5.lngMembershipNumber, '" & _
* & vDate & "' *AS CurrentDate, " & _
* "qryTemp5.FirstName, qryTemp5.LastName " & _
* FROM qryTemp5;"


On Jan 20, 6:28*pm, Bob Quintal <rquin... (AT) sPAmpatico (DOT) ca> wrote:
Quote:
Barry Edmund Wright <barry.edmund.wri... (AT) witstoronto (DOT) ca> wrote innews:72fa5219-e831-4746-abde-af0ae17e1eca (AT) m11g2000yqe (DOT) googlegroups.co
m:









Hi,

I have a combo box cboDate with the value "01/01/12" on a form. I
click a button on the same form to run a query to insert records
into a table. I want to replace the #01/01/12# value in vSQL with
the date value in cboDate? How do I do this?

Button Code:

Private Sub cmdBuildMembersTemp_Click()

* 'Append all the records in qryTemp5 to tblMembersTemp

* Dim qry As QueryDef
* Dim vSQL As String

* 'First Delete all records in tblMembersTemp
* Set qry = CurrentDb.CreateQueryDef("", "DELETE * FROM
tblMembersTemp")
* qry.Execute

* vSQL = "INSERT INTO tblMembersTemp ( lngMembershipNumber,
CurrentDate, FirstName, LastName " & _
* * * * * *"SELECT qryTemp5.lngMembershipNumber,
Format(#01/01/12#,""mmmm d, yyyy"") AS CurrentDate,
qryTemp5.FirstName, qryTemp5.LastName " & _
* * * * * *"FROM qryTemp5;"

* Set qry = CurrentDb.CreateQueryDef("", vSQL)
* qry.Execute
* qry.Close
* Set qry = Nothing

end Sub

Thanks, appreciate your help.

Barry

The table stores the date as a double precision number, with the
whole portion representing the number of days since 1899/12/31 (e.g.
Jan 1 1900 is day 1) and the fractional part gives the time (noon is
.5, 6pm is .75). Therefore, attempting to format the date will cause
problems.

You also need to make vba evaluate the contents and write the result
to the string,

Try
* vSQL = "INSERT INTO tblMembersTemp " & _
* "(lngMembershipNumber, CurrentDate, FirstName, LastName) " & _
* "SELECT qryTemp5.lngMembershipNumber, " & _
* "#" & me.cbodate & "# *AS CurrentDate, " & _
* "qryTemp5.FirstName, qryTemp5.LastName " & _
* FROM qryTemp5;"

--
Bob Q.
PA is y I've altered my address.

Reply With Quote
  #7  
Old   
Bob Quintal
 
Posts: n/a

Default Re: Pass a Combo Box value to an Insert Query - 01-21-2012 , 06:12 PM



Barry Edmund Wright <barry.edmund.wright (AT) witstoronto (DOT) ca> wrote in
news:4260a4e3-39f9-4623-a190-a91c34ccd209 (AT) do4g2000vbb (DOT) googlegroups.co
m:

Quote:
Thanks, I realized from your advice and Doug's code that the vSQL
wanted a string so I created a variable vDate as string and
populated it with the string value of the formated date in the
combo box then used that in the vSQL.

The code below worked - don't forget the single quote on both
sides of the vdate:
'" & vDate & "'

Dim vDate As String

vDate = CStr(Format(CDate(Me.cboDate), "mmmm d, yyyy"))

* vSQL = "INSERT INTO tblMembersTemp " & _
* "(lngMembershipNumber, CurrentDate, FirstName, LastName) " & _
* "SELECT qryTemp5.lngMembershipNumber, '" & _
* & vDate & "' *AS CurrentDate, " & _
* "qryTemp5.FirstName, qryTemp5.LastName " & _
* FROM qryTemp5;"


I'm happy you got it working...As to your single quotes, that tells
me that your target field, CurrentDate, is not a date field at all,
but is really a text field in the table. If it were a date, the
delimiters I used, the # marks, would be the appropriate ones.

Bob
Quote:
On Jan 20, 6:28*pm, Bob Quintal <rquin... (AT) sPAmpatico (DOT) ca> wrote:
Barry Edmund Wright <barry.edmund.wri... (AT) witstoronto (DOT) ca> wrote
innews:72f
a5219-e831-4746-abde-af0ae17e1eca (A...ooglegroups.co
m:









Hi,

I have a combo box cboDate with the value "01/01/12" on a form.
I click a button on the same form to run a query to insert
records into a table. I want to replace the #01/01/12# value in
vSQL with the date value in cboDate? How do I do this?

Button Code:

Private Sub cmdBuildMembersTemp_Click()

* 'Append all the records in qryTemp5 to tblMembersTemp

* Dim qry As QueryDef
* Dim vSQL As String

* 'First Delete all records in tblMembersTemp
* Set qry = CurrentDb.CreateQueryDef("", "DELETE * FROM
tblMembersTemp")
* qry.Execute

* vSQL = "INSERT INTO tblMembersTemp ( lngMembershipNumber,
CurrentDate, FirstName, LastName " & _
* * * * * *"SELECT qryTemp5.lngMembershipNumber,
Format(#01/01/12#,""mmmm d, yyyy"") AS CurrentDate,
qryTemp5.FirstName, qryTemp5.LastName " & _
* * * * * *"FROM qryTemp5;"

* Set qry = CurrentDb.CreateQueryDef("", vSQL)
* qry.Execute
* qry.Close
* Set qry = Nothing

end Sub

Thanks, appreciate your help.

Barry

The table stores the date as a double precision number, with the
whole portion representing the number of days since 1899/12/31
(e.g. Jan 1 1900 is day 1) and the fractional part gives the time
(noon is .5, 6pm is .75). Therefore, attempting to format the
date will cause problems.

You also need to make vba evaluate the contents and write the
result to the string,

Try
* vSQL = "INSERT INTO tblMembersTemp " & _
* "(lngMembershipNumber, CurrentDate, FirstName, LastName) " & _
* "SELECT qryTemp5.lngMembershipNumber, " & _
* "#" & me.cbodate & "# *AS CurrentDate, " & _
* "qryTemp5.FirstName, qryTemp5.LastName " & _
* FROM qryTemp5;"

--
Bob Q.
PA is y I've altered my address.



--
Bob Q.
PA is y I've altered my address.

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.