PDA

View Full Version : [SOLVED:] code a command button to move a selected record in a list box to a table



cleteh
05-25-2015, 07:03 PM
I have a form with a list box called lstCheckPayments which is populated by a query. Id like to code a command button cmdAddCheck to add the selected record in the list box to a table named tmpCheckQueue. This is access 97. Ive tried a few things and im not having any luck.

jonh
05-26-2015, 01:00 AM
CurrentDb.Execute "insert into tmpCheckQueue (FieldWhatever) values ('" & lstCheckPayments & "')"

cleteh
05-26-2015, 08:27 AM
Thanks jonh.... let's say I have 2 fields in each row of the list box (I actually have around 12 fields in each row of the list box I want transferred) called CheckNo and Amount. Would this be the correct way to code this... and this will only the transfer the selected record in the list box? Thanks, I won't be home to try it out until 8 hours from now.

CurrentDb.Execute "insert into tmpCheckQueue (CheckNo, Amount) values ('" & lstCheckPayments & "')"

jonh
05-26-2015, 09:50 AM
Listboxes don't hold records.
Simply using lstCheckPayments would copy a single value (from the 'bound column') into the sql string.
For each field you need to specify the value

CurrentDb.Execute "insert into tmpCheckQueue (CheckNo, Amount) values ('" & lstCheckPayments.column(0) & "','" & lstCheckPayments.column(1) & "')"

cleteh
05-26-2015, 06:45 PM
It adds the record but it's only adding the CheckNo if I use CurrentDb.Execute "insert into tmpCheckQueue (CheckNo, Amount) values ('" & lstCheckPayments.column(0) & "','" & lstCheckPayments.column(1) & "')"

Date is actually the second field im trying to add but if I use this line of code CurrentDb.Execute "insert into tmpCheckQueue (CheckNo, Date) values ('" & lstCheckPayments.column(0) & "','" & lstCheckPayments.column(1) & "')"

I get an error saying Syntax Error in INSERT INTO statement

cleteh
05-26-2015, 07:04 PM
In Debug when I hover over ('" & lstCheckPayments.column(0) & "','" & lstCheckPayments.column(1) & "')" it shows the correct values im trying to insert in the tmpCheckQueue table

jonh
05-26-2015, 11:30 PM
For dates, replace the single quote with #

jonh
05-26-2015, 11:32 PM
You might need to put square brackets around Date as well to tell it it's a field instead of a reserved word.

donaldsims
08-19-2015, 06:30 AM
You solved your problem? if Yes then how?