View Full Version : Basic(I'm guessing) VBA question


vbalearner
07-06-2007, 04:50 AM
Hey guys,
I'm a newbie(to the forum and to this programming...) so thanks for any help you can provide me.

Basically, I have a small database set up. I have a few tables in the database. I have one form on which I have set up some checkboxes. I also have a "go" button(command button) on the form.

I have one empty table(called codes) set up in the database as well. What I'm trying to do:
When the go button is pressed, I would like to "look at" the check boxes and depending on which ones are checked, add rows to the empty table(called codes). Basically the table only has one column, called value. It is in the database called sample(which is saved on my desktop, if it matters).

So if when the go button is clicked, the first 3 checkboxes are checked, I would like it to add value 2, 4, 7(or whatever corresponding value I assign to that checkbox basically).

Hopefully that makes sense. I'm not very familiar with the syntax to be used, thats where I think I'm running into trouble. I've been looking at all of this code online but it always seems like they are making new databases or new tables, but I want something to go into an existing table in an existing database...

Dennisk
07-06-2007, 06:12 AM
Depends on whether your form is unbound to the table or not.

this is the unbound version

Dim strSQL as string

if yourCheckBox01 = true then
strSQL="INSERT INTO YourTableName (YourColumnName) VALUES (2)"
DoCMD.RunSQL strSQL
endif


then repeat this code changing the name of the check box and the value

vbalearner
07-06-2007, 06:44 AM
Depends on whether your form is unbound to the table or not.

this is the unbound version

Dim strSQL as string

if yourCheckBox01 = true then
strSQL="INSERT INTO YourTableName (YourColumnName) VALUES (2)"
DoCMD.RunSQL strSQL
endif


then repeat this code changing the name of the check box and the value


Thanks so much, it worked!
One more question, if you don't mind, I wanted to know how to stop that message from popping up that says 'you are about to append 1 row into the table' and you have to click ok for it to continue. Also, I wanted to stop the message from popping up that says the same thing about deleting rows when I choose to do that.

Thanks!

boblarson
07-06-2007, 07:25 AM
Thanks so much, it worked!
One more question, if you don't mind, I wanted to know how to stop that message from popping up that says 'you are about to append 1 row into the table' and you have to click ok for it to continue. Also, I wanted to stop the message from popping up that says the same thing about deleting rows when I choose to do that.

Thanks!

Instead of using
DoCMD.RunSQL strSQL

use
CurrentDb.Execute strSQL

and you won't get the warnings

lukeskyslacker
08-23-2007, 12:58 PM
I'm trying to use CurrentDb.Execute to overwrite a table with a maketable query, but it errors out, saying the table already exists.

Same thing happens with or without the dbFailOnError added.

Bricks
08-23-2007, 09:03 PM
Dude this is exaclty what I'm trying to learn at the moment, but I know even less than you...how and where do you start with somehting like this and could you mail or paste your whole script in here so that i can take a look and try and understand it??

Just another learner...

boblarson
08-23-2007, 09:42 PM
Why do you need to do a make table query instead of just clearing out the old and using an append query to fill it? It is much much more efficient and causes less "bloating" of your database.

lukeskyslacker
08-24-2007, 05:48 AM
I guess I don't need to. I'll use your method.

Thanks.