Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 5th, 2003, 03:32 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default A variable for variables

Hi:

Is there any mechanism for represent a variable using another variable?

What I'd like to do is this:

Get a recordset and assign the field values to variables for populating controls on an ASP form.

Code:
Dim strVariableName As ?
Dim strFieldName As String
Dim strFirstName As String
Dim strLastName As String

If Not rsLocal.BOF And Not rsLocal.EOF Then

  For intFieldNameCounter = 0 to 1

     Select Case intFieldNameCounter
        Case 0
          strFieldName = "FirstName"
          strVariableName = strFirstName
        Case 1
          strFieldName = "LastName"
          strVariableName = strLastName
     End Select 

     strVariableName = rsLocal(strFieldName)

  Next

End If
If I can't do this, then I have to use a second 'Select Case' in order to assign the value to the individual variables. It seems like there should be a way to do what I want to do, but I haven't found it.

Any help is appreciated.

Thanks,

JK
Reply With Quote
  #2 (permalink)  
Old November 5th, 2003, 04:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: McPherson, Kansas, USA.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well not seeing the whole function it is hard to say.

I think you ought to look at passing a parameter (strVariableName) to your function. if you want to be able to change the value in your function then you will need to pass the value by reference. Then you can use the parameter in your code.

' Declare Variables
Dim intVal s Integer

' Function definition
Private Sub MyFunction(ByRef Value1 As Integer)

    Value1 = Value1 + 1

End Sub

' Call to function
intVal = 1

MyFunction(intVal)

' After the function executes intVal = 2

I think this is what you are trying to do. If not, perhaps you can explain yourself better if I have not answered your question.

Excuse any odd syntax I have been working in C++ the last couple of months and my mind is still in that mode. But, I think you should be able to follow my example.


Larry Asher
Reply With Quote
  #3 (permalink)  
Old November 5th, 2003, 05:17 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Larry:

Sorry, it was just a snippet. rsLocal is not a function, it is the name of an ADO recordset object, undeclared and unopened in the snippet. I was afraid of cluttering up the example and detracting from the real question I had. Accordingly, the value within the parentheses is a variable that represents a field (column) name from the recordset.

If the recordset consists of one record, with two fields FirstName and LastName, and the values are 'John' and 'King' respectively, then the results of the snippet are desired to be:

'John' assigned to the variable strFirstName
'King' assigned to the variable strLastName


JK
Reply With Quote
  #4 (permalink)  
Old November 5th, 2003, 05:26 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

i see your point.. you are trying to assing to the same variable n values, wich is one has a diferent name..

can you use an array to this?? i dont think there is a way to pass the adrress of a variable to another variable to make that go to that... you have address of for procedures.. but i dont think you can use it with variables..

HTH...

Gonzalo Bianchi
Reply With Quote
  #5 (permalink)  
Old November 5th, 2003, 06:55 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi:

My goal was simply to avoid using two For Next and appropriate Select Case decision structures to
loop through the fields of a recordset and

1) ensure a desired field value is not null, then assign it to a placeholder variable
2) assign the placeholder variable to the appropriate variable

I took a different approach. Rather than using a pre-defined loop, I used the Fields.Count property for the recordset and a function to determine if it is null and assign a value.

<%

If Not rsLocal.BOF Or Not rsLocal.EOF Then
  For Each fFld in rsLocal.Fields
    Select Case fFld.Name
      Case "FirstName"
        strFirstName = CheckValue(fFld)
      Case "LastName"
        strLastName = CheckValue(fFld)
      Case "Account"
        strCompany = CheckValue(fFld)
      Case "Address1"
        strAddress = CheckValue(fFld)
      Case "Address2"
        strAddress2 = CheckValue(fFld)
      Case "City"
        strCity = CheckValue(fFld)
      Case "State"
        strState = CheckValue(fFld)
      Case "Other"
        strOther = CheckValue(fFld)
      Case "PostalCode"
        strZip = CheckValue(fFld)
      Case "DomainCode"
        strCountry = CheckValue(fFld)
      Case "WorkPhone"
        strPhone = CheckValue(fFld)
        If Not IsNull(rsLocal("PrimaryMarket")) Then
          strPhone = FormatPhoneNumber(strPhone, rsLocal("PrimaryMarket"), True)
        End If
      Case "Fax"
        strFax = CheckValue(fFld)
        If Not IsNull(rsLocal("PrimaryMarket")) Then
          strFax = FormatPhoneNumber(strFax, rsLocal("PrimaryMarket"), True)
        End If
      Case "Email"
        strEmail = CheckValue(fFld)
      Case "Title"
        strTitle = CheckValue(fFld)
    End Select

  Next

%>


<%

Function CheckValue(varValue)


If Not IsNull(varValue) And Not IsEmpty(varValue) Then
    CheckValue = varValue
Else
    CheckValue = ""
End If


End Function

%>


This is probably the right way to do it.

For some background: A very long time ago I used a database program named Alpha 4 and it had a scripting language. The scripting language has something called an 'indirect variable'. It is a variable which referred to another variable.

Now, Alpha 4, if I recall, didn't have array structures, so I used the indirect variables to build a simple array by using an array base name and incrementing a numeric last character

arr1
arr2
arr3

etc.

The ability to represent a variable name with another variable was indispensable, so it always has seemed an extremely useful thing to be able to do. It probably was only a valuable thing for that limited scripting language.

Thanks,

JK
Reply With Quote
  #6 (permalink)  
Old November 6th, 2003, 05:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

I notice the <% %> in your code snippet, so is this proper VB or just ASP? If its VB you can take advantage of CallByName to do this. If its ASP you can't, but you could use GetRows to get all the Recordset data into an array.
Reply With Quote
  #7 (permalink)  
Old November 6th, 2003, 05:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Just had another idea. You could use a Scripting.Dictionary to store your data, using the field name as the key.
Code:
<%

Dim dct
Set dct = Server.CreateObject("Scripting.Dictionary")

If Not rsLocal.BOF Or Not rsLocal.EOF Then
  For Each fFld in rsLocal.Fields                
    Call dct.Add(fFld.Name, CheckValue(fFld))                 
  Next
End If

' you can then get at the data by, for example, Response.Write dct("FirstName")
%>
hth
Phil
Reply With Quote
  #8 (permalink)  
Old November 6th, 2003, 09:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: McPherson, Kansas, USA.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I really think we are missing something here. It looks to me like you are only working with one row from the recordset. Also, the only benefit I can see from your code is it may work if you do not know the column names. Then your program determines if this recordset does have a column named FirstName, etc. This would be a very generic function and hard for me to see any real use. Otherwise, your function is not being very efficient. If you have 15 columns that you are trying to retrieve data from you have to loop 15 times through your select case statement. I would be very surprised if your function was intended to be so generic to the point that you are not sure of the column names in the record you are querying. Unless, I am completely wrong I would suggest explicitly calling the column names and get out of the loop and the select case statement.

As in,

   strFirstName = rsLocal!FirstName
   etc.

and get it over with.

???







Larry Asher
Reply With Quote
  #9 (permalink)  
Old November 6th, 2003, 01:33 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Phil,

It is ASP. However, I've not run across CallByName() before. I took a quick look at it. It's not immediately obvious to me where that function is best used. I'll have to stare at it a while.

Thanks for your suggestions.

JK
Reply With Quote
  #10 (permalink)  
Old November 6th, 2003, 01:56 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Larry:

You're absolutely right about the inefficiency of the function. I realized that that just after I hit the 'Post New Reply'.

You are also right in that I am dealing with a single row recordset. It is a recordset retreived via a stored procedure using a command object to which parameters are supplied.

As for looping through the field names, you're right about that too. I thought I had good reasons for doing that, but looking at the process more closely, I wasn't getting the efficiency I thought I was getting.

The one thing that looping and using the Select Case buys is that it prevents the ASP page from failing if the stored procedure is modified by removing a field that I am expecting. This method takes what information there is in the provided recordset, uses what it can, and provides an empty string (when initializing the variables) for those fields it was expecting but did not find. However, that's probably too passive a mechanism for error handling.

Thanks for taking the time to look at this stuff in detail and provide feedback. I'm self taught and due to where I live, have never had the opportunity to work with anyone. That sure would be nice.

JK
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object variable or With block variable not set I_Chappers ASP.NET 2.0 Basics 3 June 26th, 2008 04:15 PM
Object variable or With block variable not set haidee_mccaffrey Classic ASP Professional 5 March 8th, 2007 03:34 PM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
Object variable or With block variable not set tparrish Pro VB Databases 1 May 25th, 2005 02:08 PM
Object variable or with block variable not set spacy ASP.NET 1.x and 2.0 Application Design 0 September 21st, 2004 12:19 AM



All times are GMT -4. The time now is 09:27 PM.


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