Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question about variable name parse

Status
Not open for further replies.

EricsonJ

Programmer
Mar 3, 2002
44
US
I want to set the value of a serial of variables, say, varable1, varable2,...varable10. I used the following codes to realize this, but it does not work:

Dim strVar, strSerial As String
Dim i As Integer

strVar = "varable"
i = 1
For i = 1 To 10
strVar = strVar & Trim(Str(i))
strVar = i
Next i
VB set strVar to i , not varable1, ... varable10.

How would I do to realize this?

Thanks a lot.
 
Okay - you know why VB made strVar = i, yes?

So probably the most useful thing for you is a variable array
There is quite a good introduction to these at


You also know that strVar as you've declared it as actually a Variant data type, yes? It is a good idea to declare this explicitly and to use the correct prefix

dim varVar as Variant

or

dim strVar as string

Reply if you need more help

Mark
 
I see now why you thought strVar was a string, but in VB, unlike other langs multiple declarations on the same line are specified explicitly.

Otherwise each variable defaults to whatever has been declared as DefType (if nothing has been decalared then Variant) - this gets quite complicated so it's best in my view to stick to explicit declaration. Stay away from DefType statements

Mark
 
Thank you very much for your reply.

What I really want to do is to grant a user his right to access the menu. I stored the userID and its rights in a table, as follow(The first line is column name, the DB is MS SQL Server):
employeeID menuItem Checked Enabled Visible
010100010001 mBOM 1 1 0
010100010001 mBOO 0 0 0
010100010001 mCRP 0 0 0
010100010001 mMRP 0 0 0

I use the following codes to set the access right to the menu:
SQLQuery = "SELECT * from tMenuItemGrant WHERE employeeID='" & strEmployeeID & "'"
Set sqlResult = cn.Execute(SQLQuery)

If sqlResult.EOF = False Or sqlResult.BOF = False Then
sqlResult.MoveFirst
Else
Exit Sub
End If

Do While sqlResult.EOF = False
strMenuItemWhole = "frmERP."

strMenuItem = sqlResult("menuItem")
bolChecked = sqlResult("Checked")
bolEnabled = sqlResult("Enabled")
bolVisible = sqlResult("Visible")

If Len(Trim(strMenuItem)) > 0 Then '* The menuitem is not a blank one
strMenuItemWhole = strMenuItemWhole & strMenuItem & "."

strMenuItemWhole = strMenuItemWhole & "Checked"
strMenuItemWhole = bolChecked

strMenuItemWhole = strMenuItemWhole & "Enabled"
strMenuItemWhole = bolEnabled

strMenuItemWhole = strMenuItemWhole & "Visible"
strMenuItemWhole = bolVisible
End If
sqlResult.MoveNext
Loop

I want it to realize the following function:

frmERP.mBOM.Checked=False
frmERP.mBOM.Enabled=False
.....

But It does not work as what I wanted.
 
Oops. I just thought you didn't know about arrays. But I don't actually see how your initial post relates to your last one. I don't know too much about DB work yet so maybe someone else can help.
Mark
 
No offense intended, but your approaching this in completely the wrong. You're trying to get strMenuItemWhole to evaluate to an object, which just isn't possible. To clarify, you're trying to build the string "frmERP.mBOM.Enabled" to represent the Enabled property of the menu item, and then set that property of that control to false through the string. You can't do that in VB. In FoxPro, maybe, but not VB. There is no macro substitution. You're just assinging values to a string variable, and in VB a string is just a string - you can't make it evaluate to a control or variable. I hope that makes sense.

The closest thing VB has to what you want to do is to use the Controls collection of the Form object. For example, you use the following line
frmERP.Controls(strMenuItem).Checked=bolChecked
In this case, you would be using the string variable strMenuItem to specify the name of a collection member, which evaluates to a control. Then you can access the Checked property of that control and set it to whatever value you retreived.
 
Thanks a lot. It works. I used to use script language, and just began to use VB 2 monthes ago, so I think I can realize this in VB by sth like &VarableName. :)

Thank you again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top