Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 17th, 2008, 05:14 AM
Authorized User
 
Join Date: Feb 2008
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to hewstone999
Default Loop through a Table and return the value

Loop through a Table and return the value


I have a table (ExportTables) with the following data

ExportTables
--------------------
MPI_CODE
MPI_IDS
MPI_REFF
REFFERALS
NOK_AD
--------------------

I want some VBA code that can loop through the table above one by one returning the value. The table data will change so i would like the code to handle change as well.

I want the returning value to be returned has a string i.e. TblValue = <table data value>

Because then i want to use the value to be put in this sql query- DoCmd.RunSQL "INSERT INTO TEST_DOC SELECT * FROM " & TblValue

then loop onto the next value.

Hope you understand what im after.


 
Old March 17th, 2008, 06:38 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Nope.

Can you post some sample data in the before state, in the after state, and what you want to achieve with the Insert statement?

Do you want to "loop" through rows or columns? How do you want to capture the values, etc? What values do you want to capture?

Also, what do you mean you want the code to handle change?



mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old March 17th, 2008, 06:45 AM
Authorized User
 
Join Date: Feb 2008
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to hewstone999
Default

What i would like is something like this:

Start loop

TblValue = ExportTables.Value
DoCmd.RunSQL "INSERT INTO TEST_DOC SELECT * FROM " & TblValue

'get value in table

End loop


I want to loop through the rows has the table only has one column.

The data in the table i.e. MPI_CORE will change so i need some code that can handle this.

The insert statement will just insert the value from the table in another table has the data in the table contain table names.





 
Old March 17th, 2008, 06:47 AM
Authorized User
 
Join Date: Feb 2008
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to hewstone999
Default

'get value in table
 sorry i meant
'next value in table

 
Old March 17th, 2008, 06:55 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I am still not exactly sure what you want to do. Here is what I would do using ADO:

Dim rs As ADODB.Recordset
Dim sSQL As String
Dim sValue As String

sSQL = "SELECT * FROM ExportTables"
Set rs = New ADODB.Recordset
rs.Open sSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic

DoCmd.SetWarnings False
rs.MoveFirst

Do Until rs.EOF
   sValue = rs("ColumnName")
   sSQL = "INSERT INTO TEST_DOC SELECT * FROM " & sValue
   DoCmd.RunSQL sSQL

rs.MoveNext
Loop
DoCmd.SetWarnings True
rs.Close

Did that help?




mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old March 18th, 2008, 05:23 AM
Authorized User
 
Join Date: Feb 2008
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to hewstone999
Default

Yes thank you. I changed the code a little to suit my purpose but the general idea was good.

Thank you :D






Similar Threads
Thread Thread Starter Forum Replies Last Post
DTS return table ID forkhead SQL Server DTS 2 November 9th, 2006 12:15 PM
RETURN IDENTITY AFTER INSERT INTO SECOND TABLE pallone SQL Language 2 August 19th, 2006 05:51 PM
Return from different records in same table rodmcleay SQL Language 2 October 10th, 2005 08:51 PM
could i join function(return table) with a table alyeng2000 SQL Server 2000 6 September 30th, 2004 07:23 AM
Return a Record set into a table? morpheus Classic ASP Basics 2 November 18th, 2003 11:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.