Adding both text and an ID value to a VB6 combobox - vb6

I am currently trying to add to my VB6 combobox using the AddItem method. This works, however, I want to display text in the drop down but I need to pass the ID of that text.
Is there a way to accomplish this by using the AddItem method?

It can't be done in the AddItem method but it's fairly easy to do it immediately after, using the NewIndex property, as long as the ID is a numeric value:
With Combo1
For i = 16 To 34
.AddItem "Item " & i
.ItemData(.NewIndex) = i
Next
End With

As the ID was not numeric I didn't use the solution above.
I had to create a type that had a "desc" and an "cod" and then create an array of that type.
I then used the ListIndex of the drop down (populated by the array) to get the element value which contained the id.
Private Type T_arrType
cod As String
dsc As String
End Type
dim x as integer
x = cbo.listIndex
msgbox(strArr(x).cod)
msgbox(strArr(x).dsc)

Related

Expected Function or variable vb6.0

Hello i am coding in Visual Basic 6.0 and i have this error, i am trying to make a button inserting data in database.
Expected Function or variable
This is my code
Private Sub Command1_Click()
With ConString.Recordset.AddNew
!ID = txtID
!Emri = txtEmri
!Mbiemri = txtMbiemri
!Datelindja = txtData
!Telefon = !txtTelefon
!Gjinia = gender
!Punesuar = job
!Martese = cmbMartese
!Vendlindja = txtVendlindje
End With
End Sub
txtID is textbox
txtEmri is textbox
txtMbiemri is textbox
txtData is date picker
txtTelefon is textbox
gender is a string that takes value if a radio button is clicked
job is an integer if a checkbox is clicked
cmbMartese is combo box
txtVendlindje is textbox
Thank you in advance.
#JohnEason gave you the right answer. But there's another error in the line !Telefon = !txtTelefon, if I'm not mistaken. It should read !Telefon = txtTelefon, i.e. without the exclamation mark in front of txtTelefon.
I'm also not a big fan of that coding style. I prefer to not rely on default properties, but instead "spell out" the whole term, e.g.
With ConString.Recordset
.AddNew
!ID = txtID.Text
!Emri = txtEmri.Text
!Mbiemri = txtMbiemri.Text
!Datelindja = txtData.Text
!Telefon = txtTelefon.Text
' Since VB6 doesn't provide Intellisense for variables, I prefer to use some
' kind of hungarian notation for variable names, in this case sGender or strGender for a string variable
!Gjinia = gender
' Same here: iJob or intJob for an integer variable
!Punesuar = job
' You need to specify that you want the selected item of a combobox
!Martese = cmbMartese.List(cmbMartese.ListIndex)
!Vendlindja = txtVendlindje.Text
' If you're done setting column values and don't do so somewhere else,
' you also need to add a .Update statement here in order to finish the
' .AddNew action and actually persist the data to the database.
' .Update
End With
End Sub
As pointed out below by #JohnEason, there's also potentially an .Update statement missing within the With/End With block

How to set in Report the TextBox visibility by expression in VisualStudio?

I have a simple TextBox in my Precision Design related to a Field in Temporary Table.
I need to show this TextBox only If the it value under Temporary Table is pupulated : so if the value field is pupulated (is a string) I show the Text Box , otherwise I don't show the text Box.
I need to set Visibility by Expression :
Which is the best way forward over ?
Thanks!
You can use iif function. Press fx button, here you can writte your code.
iif function evaluate a condition and return 1 of 2 options, for example, in your case you need show one value only if exist.
check this code:
=iif(fields!YourFieldName.value = "", true, false)
if your field is a number
=iif(fields!YourFieldName.value = 0, true, false)
This code evaluate the value of your field and only populate the value if is complete.

Populating a control from an enumeration

Say I have the following enumeration:
Public Enum TimeUnit
Day
Week
Month
Year
End Enum
Is there any way I can use this enumeration to populate a ComboBox or ListBox? Ideally, I'd like the control to echo the internal representation as the inserted value (i.e. Day, not 0).
If there is a better way to go about this (c.f. 'X/Y Problem'), my basic requirement is that I must be able to use TimeUnit similarly to an enumeration, i.e. as a type in itself. I would also really like to avoid repeating myself in the code.
I am assuming you want to use the enum to pass as a value to some method. You can assign the enum value to the ItemData property, and literal values to the Item (text the user sees). I would suggest using Bob77's naming convention and writing a method to populate your combobox or listbox control. Something like below should work.
Private Sub LoadCombo()
Combo1.Clear
Combo1.AddItem "Day"
Combo1.ItemData(Combo1.NewIndex) = TimeUnit.Day
Combo1.AddItem "Week"
Combo1.ItemData(Combo1.NewIndex) = TimeUnit.Week
Combo1.AddItem "Month"
Combo1.ItemData(Combo1.NewIndex) = TimeUnit.Month
Combo1.AddItem "Year"
Combo1.ItemData(Combo1.NewIndex) = TimeUnit.Year
End Sub
Private Sub Combo1_Click()
MsgBox "You have selected " & Combo1.Text & " (" & CStr(Combo1.ItemData(Combo1.ListIndex)) & ")"
End Sub

Getting model value and generated ID from within simple_form custom input

I'm trying to make a custom input type with simple_form that will implement combobox-type functionality using jQuery-Autocomplete
. What I need to do is output a hidden field that will hold the ID of the value selected and a text field for the user to type in.
Here's what I have so far:
class ComboboxInput < SimpleForm::Inputs::Base
def input
html = #builder.hidden_field(attribute_name, input_html_options)
id = '' #what?
value = '' #what?
return "#{html}<input class='combobox-entry' data-id-input='#{id}' value='#{value}'".html_safe
end
end
I need to get the ID of the hidden field that simple_form is generating to place as an HTML attribute on the text entry to allow the JavaScript to "hook up" the two fields. I also need to get the value from the model to prepopulate the text input. How do I do this from within my custom input?
I'm looking for the id as well, but I did get the value:
def input
current_value = object.send("#{attribute_name}")
end
I just found a hokey id workaround:
html = #builder.hidden_field(attribute_name, input_html_options)
id = html.scan(/id="([^"]*)"/).first.first.to_s
I know it's a hack, but it does work. Since we don't have access directly to this type of resolution, it is likely to keep working even if the underlying id creation code changes.

vb6 call form with name contained in array

I have an Array named Menus. It contains a form name per element.
How can I call them dynamically?
For example, if Menus(1) = "Login", and Menus(2) = "Logout" I need to say
Login.Show
but I want to do this using the Array name. I clearly can't do this:
Menus(X).Show
Is this possible in VB or is there a way around this?
Thanks in advance!
What you're essentially trying to do is use a form's name to instantiate and load a form.
One way to do this is to pass a string with your form's name to the Form Collection's Add function:
Dim f As Form
Set f = Forms.Add(Menus(X))
f.Show
Or, using VB6's CallByName Function:
Dim f As Form
Set f = CallByName(Forms, "Add", VbMethod, Menus(X))
f.Show
You can use the following code:
Form form = Menus[x] as Form
Form.show

Resources