Solved

Assign A Variable to rst!(Variable)

Posted on 2001-08-06
10
592 Views
Last Modified: 2006-11-17
Hello,

I want to use a variable to assign a field name. Can someone explain to me how I would go about it?
Here's the code.

Dim strTableName as string
Dim fldName as String
Dim i_strTextBoxItem as String

TblName = "tblRevenue"
FldName = "Revenue"
i_strTextBoxItem = "Great"

If Not App_Conn.State = adStateOpen Then
          App_Conn.Provider = App_Provider '"Microsoft.Jet.OLEDB.4.0" 'App_Provider
          App_Conn.ConnectionString = App_InventoryPath ' C:\DS3\DS3_Data\DS3.MDB"
          App_Conn.Open
End If

' Open the recordset
rst.Open "SELECT * FROM " & strTableName, _
App_Conn, adOpenKeyset, adLockOptimistic
     
rst.AddNew                    ' Add a new record

******************* Problem Area here *************

rst!fldName = i_strTextBoxItem          'Specify the values for the fields
     
'Revenue is the name of the field that is being assigned to fldName variable. This field name
will change - that is why I need to assign it to a variable.

rst.Update                    ' Save the current record in the Recordset
rst.Close                    ' Close the recordset

-ADawn
0
Comment
Question by:ADawn
  • 5
  • 4
10 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 30 total points
ID: 6357127
Use the fields collection directly:

rst.Fields(i_strTextBoxItem).Value = "My New Value"
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6357169
>rst.Fields(i_strTextBoxItem).Value = "My New Value"

Tim is 100% correct, but in this particular example, fldName stores the field's name, so you would write it as follows:

   rst.Fields(fldName).Value = i_strTextBoxItem

Since i_strTextBoxItem equals "Great" and fldName equals "Revenue", this assigns the value "Great" to the field named "Revenue".


Give Tim the points; I just thought I'd help clarify.

-Dennis Borg
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6357175
>rst.Fields(fldName).Value = i_strTextBoxItem

The abreviated form of this is:

   rst(fldName) = i_strTextBoxItem


0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 43

Expert Comment

by:TimCottee
ID: 6357276
Cheers Dennis, tag team again? I must admit that I am a little tired right now so probably not looking hard enough at the code. It is 10:15pm here and I still have 8 hours to go!
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6357966
Tim:

>Cheers Dennis, tag team again?


Yep!  Get some sleep, Tim; tomorrow is *your* turn!  ;-)

There are times the code looks blurry to me too!  ;-)


-Dennis Borg
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6358125
Get some sleep, I wish I am here on the nightshift (10pm to 6am) to babysit users through an application roll-out. Only half an hour to go and then I can head to my bed!
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6359798
Hope the rollout goes well, and that you don't have any real problems.

Is the night shift your normal shift? Or are you there only because of the roll-out?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6360743
I was only there on the nightshift because of the rollout, all went well as far as that goes. I normally work days though one week in 8 I cover out-of-hours support which basically means I have to leave the mobile phone on 24/7 and answer it if it rings so it generally isn't too much of a problem. I just hate having to do odd shifts that screw up my normal pattern especially as I don't get overtime for it, but at least I do get a couple of days of instead.
0
 

Author Comment

by:ADawn
ID: 6361063
Thank TimCottee,

DennisBorg: Thanks for your input. I figured out what Tim was conveying in him first comment.

-ADawn
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6361886
You're welcome, ADawn!  Glad to have helped and that you got it working.

-Dennis Borg
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question