Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Make a table with VBA (Access 2003)

2 views
Skip to first unread message

tg112001 via AccessMonster.com

unread,
Nov 8, 2007, 8:26:37 PM11/8/07
to
Hello,

I have a form with a combo box (named Combo1) and a command button. The
purpose of the form is to make a new table based on the combo box value after
clicking the command button. Can someone help me with the proper syntax?

The recordset should look like:

SELECT * FROM tbl1 WHERE field1 = Combo1.value

But I don't know how to use that recordset to make a new table. Can someone
help me with the correct VBA syntax to make this happen?

Thanks!

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/Forums.aspx/access-modules/200711/1

NevilleT

unread,
Nov 8, 2007, 8:49:00 PM11/8/07
to
Hi
Use something like this

Dim dbs as database
Dim tdfNew as tabledef
Dim strSQL as String
Dim lngFirst as Long
Dim lngSecond as Long

lngFirst = rst!FirstValue
lngSecond = rst!SecondValue

Set dbs = CurrentDb
Set tdfNew = dbs.CreateTableDef("tblNew") ' Set the table name

With tdfNew
.Fields.Append .CreateField("Field1", dbLong) ' Create the
field
.Fields.Append .CreateField("Field2", dbLong) ' Create
the field
dbs.TableDefs.Append tdfNew ' Append
the table

Set idxNew = .CreateIndex("NumIndex")
idxNew.Fields.Append idxNew.CreateField("Field1")
idxNew.Primary = True
.Indexes.Append idxNew
End With

strSQL = "INSERT into tblNew (Field1, Field2) VALUES( " & lngFirst &
", " & lngSecond & ";"

Set qdf = dbs.CreateQueryDef("", strSQL) ' Create
new QueryDef.
qdf.Execute ' Run
the insert query

Dirk Goldgar

unread,
Nov 8, 2007, 10:09:00 PM11/8/07
to
In news:7aee9c48822e6@uwe,

tg112001 via AccessMonster.com <u34664@uwe> wrote:
> Hello,
>
> I have a form with a combo box (named Combo1) and a command button.
> The purpose of the form is to make a new table based on the combo box
> value after clicking the command button. Can someone help me with
> the proper syntax?
>
> The recordset should look like:
>
> SELECT * FROM tbl1 WHERE field1 = Combo1.value
>
> But I don't know how to use that recordset to make a new table. Can
> someone help me with the correct VBA syntax to make this happen?

Here's a pretty simple way:

CurrentDb.Execute _
"SELECT * INTO NewTableName FROM tbl1 " & _
"WHERE field1 = " & Combo1.value,
dbFailOnError

If field1 is a text field, use

"WHERE field1 = '" & Combo1.value & "'",

(assuming the combo's value won't contain the single-quote character
(') ).

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


tg112001 via AccessMonster.com

unread,
Nov 9, 2007, 7:14:25 PM11/9/07
to
Thanks I'll give it a try and will let you know!

Dirk Goldgar wrote:
>> Hello,
>>
>[quoted text clipped - 9 lines]


>> But I don't know how to use that recordset to make a new table. Can
>> someone help me with the correct VBA syntax to make this happen?
>
>Here's a pretty simple way:
>
> CurrentDb.Execute _
> "SELECT * INTO NewTableName FROM tbl1 " & _
> "WHERE field1 = " & Combo1.value,
> dbFailOnError
>
>If field1 is a text field, use
>
> "WHERE field1 = '" & Combo1.value & "'",
>
>(assuming the combo's value won't contain the single-quote character
>(') ).
>

--
Message posted via http://www.accessmonster.com

tg112001 via AccessMonster.com

unread,
Nov 9, 2007, 9:19:20 PM11/9/07
to
Hey Dirk,

The code almost works! I probably should have told you more about tbl1.

tbl1 resides in a seperate backend .mdb which holds the data on a network
server.
The frontend .mdb (where this form resides) has a link to tbl1

So it seems that the code partially works. When the code executes, it
creates the table structure with correct data types, etc., but doesn't bring
in the data.

Anything that you can suggest??

Tom


Dirk Goldgar wrote:
>> Hello,
>>
>[quoted text clipped - 9 lines]

>> But I don't know how to use that recordset to make a new table. Can
>> someone help me with the correct VBA syntax to make this happen?
>
>Here's a pretty simple way:
>
> CurrentDb.Execute _
> "SELECT * INTO NewTableName FROM tbl1 " & _
> "WHERE field1 = " & Combo1.value,
> dbFailOnError
>
>If field1 is a text field, use
>
> "WHERE field1 = '" & Combo1.value & "'",
>
>(assuming the combo's value won't contain the single-quote character
>(') ).
>

--

Dirk Goldgar

unread,
Nov 10, 2007, 2:32:59 AM11/10/07
to
In news:7afba4f502a6a@uwe,

tg112001 via AccessMonster.com <u34664@uwe> wrote:
> Hey Dirk,
>
> The code almost works! I probably should have told you more about
> tbl1.
>
> tbl1 resides in a seperate backend .mdb which holds the data on a
> network server.
> The frontend .mdb (where this form resides) has a link to tbl1
>
> So it seems that the code partially works. When the code executes, it
> creates the table structure with correct data types, etc., but
> doesn't bring in the data.
>
> Anything that you can suggest??
>
> Tom

That suggests that the WHERE clause of your query didn't return any
records. Check the value of Combo1, taking into account the possibility
that the combo is displaying one column of its rowsource but bound to
another. Make sure that there really are records in tbl1 that have that
value in field1. Check the data type of field1 to make sure you use
quotes or don't use them, appropriately, in building the SQL statement.

tg112001 via AccessMonster.com

unread,
Nov 13, 2007, 6:10:44 PM11/13/07
to
Dirk,
Thanks for your help. I got finally got it to work. It was the field name
that was causing the problems!

Dirk Goldgar wrote:
>> Hey Dirk,
>>
>[quoted text clipped - 12 lines]


>>
>> Tom
>
>That suggests that the WHERE clause of your query didn't return any
>records. Check the value of Combo1, taking into account the possibility
>that the combo is displaying one column of its rowsource but bound to
>another. Make sure that there really are records in tbl1 that have that
>value in field1. Check the data type of field1 to make sure you use
>quotes or don't use them, appropriately, in building the SQL statement.
>

--

0 new messages