473,472 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Call Variables stored in Table

Hey there folks,

I was wondering if there was a way to store a list of variables in a
table and then call them one at a time in some loop method.

Here's what I've got:

A table "Tab Names" with 50 values all running down the first column
named "Tabs"

I then have a snippet of VBA that executes a SQL statement to make a
table.
- The SQL has a variable in it "tName"
- The SQL is run through the execute command not DoCmd.RunSQL

Is there a way to look to the table, grab the first variable, run the
SQL with that variable, and then loop through the next 49 variables
until the last variable in the table.

Thanks in advance for you expertice,

Oct 25 '06 #1
4 4638

hm*****@hartford.edu wrote:
Hey there folks,

I was wondering if there was a way to store a list of variables in a
table and then call them one at a time in some loop method.

Here's what I've got:

A table "Tab Names" with 50 values all running down the first column
named "Tabs"

I then have a snippet of VBA that executes a SQL statement to make a
table.
- The SQL has a variable in it "tName"
- The SQL is run through the execute command not DoCmd.RunSQL

Is there a way to look to the table, grab the first variable, run the
SQL with that variable, and then loop through the next 49 variables
until the last variable in the table.

Thanks in advance for you expertise
Sure, not hard at all.

Public Sub ProcessSQLs()
dim rs as DAO.Recordset
dim strSQL As String

'--read the individual field values in the column...
set rs = DBEngine(0)(0).OpenRecordset("SELECT [Tabs] FROM [Tab
Names]", dbOpenSnapshot)

do until rs.EOF
'---build SQL statement in a string variable
strSQL = "INSERT INTO... WHERE SomeField=" & cQUOTE &
rs.Fields("tName") & cQUOTE

'--Execute the statement
DBEngine(0)(0).Execute strSQL, dbFailOnError
'--Move to next record
rs.MoveNext
loop '---repeat with each record in the recorset

'--clean up variables.
rs.close
set rs=nothing

End Sub

Oct 25 '06 #2
The logic of what you are doing is, to say the least, "unusual." If you
explain what you have and what you are trying to accomplish, there's a
really good chance that someone will be able to suggest a simpler approach
than creating so many (temporary?) tables. At best, if you do this, you
will have to Compact and Repair frequently to prevent database bloat.

However, to answer your question: You can construct the entire SQL
statement, and probably will only need a couple of concatenations. The
"source" of a Db.Execute is a string. Here's a quick example, using a
delete query because it was simpler... you code your own loop, table read,
etc. use your own text as basis for the SQL, and retrieve your table name
from your "table of tablenames." For the query you describe, you won't need
a WHERE clause. "tblDelMe" is a little test table with a text field for
primary key.

Function ExecuteQuery(pstrK As String) As Boolean
On Error GoTo Proc_Error
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strQ As String
Dim strT As String
strT = "tblDelMe"
strQ = "DELETE * FROM " & strT & " WHERE [ID] = """ & pstrK & """"
Set db = CurrentDb()
db.Execute (strQ)
ExecuteQuery = True
Proc_Exit:
On Error GoTo 0
Exit Function

Proc_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure ExecuteQuery of Module basDAOMiscCode"
ExecuteQuery = False
Resume Proc_Exit
End Function

Larry Linson
Microsoft Access MVP

<hm*****@hartford.eduwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Hey there folks,

I was wondering if there was a way to store a list of variables in a
table and then call them one at a time in some loop method.

Here's what I've got:

A table "Tab Names" with 50 values all running down the first column
named "Tabs"

I then have a snippet of VBA that executes a SQL statement to make a
table.
- The SQL has a variable in it "tName"
- The SQL is run through the execute command not DoCmd.RunSQL

Is there a way to look to the table, grab the first variable, run the
SQL with that variable, and then loop through the next 49 variables
until the last variable in the table.

Thanks in advance for you expertice,

Oct 25 '06 #3
Larry,

I am interested to learn of another way to minimize DB bloat. Here's a
little more background, let me know if my thinking is correct. I've
only been working with VBA for two months now.

I need to allow people who don't know how to code in VBA the ability to
change the variables in my table "Tab Names". My ultimate goal is to
create a form that would allow other Non-VBA savye employees to add or
delete companies from the table.

The table is a list of 50 or so company codes, ie WalMart. The SQL I
have takes these variables and creates andexports an Excel Worksheet
with the worksheet tab named as the variable, ie Walmart. The SQL also
uses the variable to select only the records in the main table with
that name. So there's a big table with a bunch of company info, then
the SQL resultant selects only the records with that company name. It's
just a way to seperate out the data for ease of analysis.

If you folks know of a way to minimize the number of tables, and bloat
while still allowing for a end Form alteration of the variable on the
list.

I really appreicate your help her everyone, this is my number one
source for "to the point" VBA help, thanks!

Oct 25 '06 #4
Just in case any one would like to see the final solution I settled on
here it is:

Public Sub Company_Tab()
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strComps As String
Set rs = CurrentDb.OpenRecordset("Tab Names", dbOpenSnapshot)

Do Until rs.EOF

strComps = rs.Fields("Tab Name")

strSQL = "SELECT Companies." & strComps & "INTO tbl" & strComps"

CurrentDb.Execute strSQL, dbFailonError

rs.MoveNext

Loop

rs.Close

Set rs = Nothing
End Sub

Oct 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Titus Cheung | last post by:
Hello, I want to write some code such that it'll update (ie insert, delete, etc) some data to/from a mySQL database whenever a user hit an HTML form button (ie Submit). Now what I find annoying...
7
by: TinTin | last post by:
Hello, How do I concatinate a variable. Here's the scenarios: declare @var1 varchar(20) declare @var2 varchar(20) declare @var3 varchar(20) declare @var4 varchar(20) .. ..
8
by: Lucas Lemmens | last post by:
Dear pythonians, I've been reading/thinking about the famous function call speedup trick where you use a function in the local context to represent a "remoter" function to speed up the 'function...
0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
1
by: RSH | last post by:
Im trying to create a stored procedure of the following code. I want to set it so that I have to send the first 4 variables (@DB, @BackUpFile,@TestDB,@RestoreFile). I am having trouble when i try...
10
by: EOZyo | last post by:
Hi, i'm trying to set pagination for a search i run on my website, i'll try to explain the easiest i can: When i click the search button on search.php, data is received and stored in variables...
3
by: mandible | last post by:
I'm trying to call one stored procedure inside another. I was wondering if this is possible Some ideas I was toying with is putting the first stored procedure inside of a temp table but haven't...
1
by: chaitanya02 | last post by:
Hi All, Well- this question might have appeared several times on this forum- but would appreciate your reply on this: I have a asp page, where customers login with some username and the pwd,...
2
by: Brad Pears | last post by:
I am working on a vb.net 2005 project using sql server 2000 as the backend . I am having a bit of problems with date variables... Here is the scenario... I have a table that includes a couple...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.