Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics 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 June 1st, 2004, 06:14 AM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Variable Recordset Field Name

I'm printing to a text file the contents of a user selected database and field.

How do I get round the fact that they could have selected any field?

e.g.
string = rs!variable
write #1 string

where variable is the name of a string variable not a recordset field called variable

 
Old June 1st, 2004, 06:24 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to acdsky
Default

Hi

You could get the field names from the recorset. Use for example:

For Each Field In RS.Fields
        varA = (Field.Name)
        Debug.Print (varA)
Next

Let me know if you need more detail

Regards
Marnus
 
Old June 1st, 2004, 06:33 AM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I've got the field names using TableDef and populate a listbox with each table name and relevant field name.

But then whichever field is selected I want to print the distinct values to a text file.

It's just how do I write the content of just the selected field?



 
Old June 1st, 2004, 07:00 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to acdsky
Default

To get the value that has been selected in the listbox use:

For X = 0 To List1.ListCount - 1
             ' If the item is selected...
             If List1.Selected(X) = True Then
                'Get the Selected item.
                varField = (List1.List(X))
             End If
Next X

With this you will end up with the selected field name in the listbox, in varField. You could then use this to query the DB to get the data and loop through the values and write them to text file.
 
Old June 1st, 2004, 07:07 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to acdsky
Default

Just to add on to that...

You will have the field name in varField. Assuming that you have already run the query use:

Do Until RS.EOF
Z = RS(varField)
"""write Z to txt file using fso"""
rs.movenext
Loop

Is this what u are looking for?
 
Old June 1st, 2004, 07:13 AM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

acdsky,

I agree with what you have said and have done something similar.

I populate my list box with each table name field name then query the database with a statement:

"select distinct " & gstrF & " from " & gstrTB

Which gathers the distinct values within the user selected field (gstrF) from the user selected table (gstrTB).

But my problem is simply outputting those recordset values via a loop to a text file
. There is only one field in the recordset.

For i = 1 To rs.RecordCount - 1
        strText = rs!variable
        Write #1, strText
        rs.MoveNext
Next

Where variable is the name of a string variable (gstrF) taken from the listbox selected field name - not a recordset field called variable



 
Old June 1st, 2004, 07:15 AM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes!!

That was all I wanted the RS(varfield). I just couldn't think of the format avoiding rs!varfield

Thanks!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable used in recordset field name the4barkersab Access VBA 1 February 24th, 2008 04:18 PM
Changeing a Recordset Name With a Variable? Winners Access VBA 2 August 22nd, 2007 02:10 PM
Create a dynamic variable from a recordset tdaustin Classic ASP Basics 3 May 13th, 2004 11:04 PM
assign a recordset to a variable biggen55 VB How-To 1 March 26th, 2004 02:26 PM





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