• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Easy visual basic question

LeStEr

Diamond Member
Right now I have labels displaying the value of fields from an access database. I want to know how I can take the value in the label and place it in a variable.

Thanks
 
I think he wants the actual data, not the physical name of the label. The initial question wasn't quite right since you actually use text boxes to display records, not labels. The labels just display the titles of the text boxes.

At least that's what I think he means.
 
labelname.caption dosnt work because im not actually changing the caption. Im just using a label to display data. I tried displaying the data with a text box and trying variable = textname.text but that didnt work. Im a little confused myself why variabel = lblname dosnt work. Thanks for the replys. If anyone has any ideas on how to just take data from fields in a database im all ears.
 
how are you getting the value of the db fields into the labels/textboxes? where ever you are doing this, can't you just assign your varible to be the same thing that you are assigning label.caption or textbox.text?
 
Im a little confused myself why variabel = lblname dosnt work
It doesn't work because it's illegal - labels can't have values. You need to use a textbox, dropdown or any of the other controls that have dynamic data. Labels are static and fixed.

If anyone has any ideas on how to just take data from fields in a database im all ears.
Like I said before, variablename = [field] will work just fine. Just refer to the field name although you could also use the name of the textbox if it was different (usually it isn't though). Note that you must use the square brackets when referring to fields or controls.

I've just tried it now (on a textbox with a field bound to it) and it worked perfectly.
 
Originally posted by: BFG10K
Im a little confused myself why variabel = lblname dosnt work
It doesn't work because it's illegal - labels can't have values. You need to use a textbox, dropdown or any of the other controls that have dynamic data. Labels are static and fixed.

If anyone has any ideas on how to just take data from fields in a database im all ears.
Like I said before, variablename = [field] will work just fine. Just refer to the field name although you could also use the name of the textbox if it was different (usually it isn't though). Note that you must use the square brackets when referring to fields or controls.

I've just tried it now (on a textbox with a field bound to it) and it worked perfectly.

Uhh, no. This isn't VBA 🙂
 
Ok this is my main question now.

If I have a text box displaying a datafield from a datasource how do I get the data displayed in the text box into a variable?

 
Uhh, no. This isn't VBA
I don't know what you mean. If you don't use square brackets when referring to fields or controls Access thinks you're referring to variables and will give you an error about them not being defined.
 
Originally posted by: BFG10K

It doesn't work because it's illegal - labels can't have values. You need to use a textbox, dropdown or any of the other controls that have dynamic data. Labels are static and fixed.

I think this is where the confusion is. A label in VB is an object that is NOT static, that is, it's properties CAN be manipulated.
 
Of course label properties can be manipulated, labels just can't have any data attached to them. If you want to do that in Access you need anything other than a label - textbox, dropdown etc.

If you click on the properties of a label the "data" tab is empty for this very reason. There is no way you can do something like assign records to a label from a query. I guess in theory you could try and stick the data into the caption section but that's just clumsy.
 
Originally posted by: BFG10K
Of course label properties can be manipulated, labels just can't have any data attached to them. If you want to do that in Access you need anything other than a label - textbox, dropdown etc.

If you click on the properties of a label the "data" tab is empty for this very reason. There is no way you can do something like assign records to a label from a query. I guess in theory you could try and stick the data into the caption section but that's just clumsy.

LeStEr,
You weren't very clear. Are you working in VB or Access. They are not the same. You said you were displaying data from an Access db, but that doesn't mean you are working in Access.

If this was a VB question,
I use labels to display data all the time. If you are displaying something and you don't want it to be or look like an editable field, it is much simpler than using a textbox and setting locked = true or enabled = false, then changing the backcolor to match the form and changing the border to transparent.

And you don't need to say lbl1.caption or txt1.text because those are the default properties for those controls.
Of course it is good programming practice to specify the property you are referring to rather than relying on defaults.

Dim strMyString as string
lbl1.caption = rs!FieldName 'no brackets needed unless the field name contains spaces
strMyString = lbl1.caption


 
VB 6
Dim anandtech As ADODB.Recordset
Set anandtech = New ADODB.Recordset

anandtech.Open "Select Top 1 * From Main SomeTable", YourConnStr, 2, 2

If anandtech.EOF = False Then
YourLabel.Caption = anandtech.Fields("FieldName").Value
End If


VB .NET
Dim anandtech As ADODB.Recordset = New ADODB.Recordset

anandtech.Open "Select Top 1 * From Main SomeTable", YourConnStr, 2, 2

If anandtech.EOF = False Then
YourLabel.Text = anandtech.Fields("FieldName").Value
End If
 
Back
Top