Check value in dropdown - validation

I want to check the value in a dropdown list. The list is preconfigured to hold a yes or a no.
At the moment, I am using a check box, that looks like this:
If chkboxOne.Value = vbChecked And (LenB(txtDetailsRefNo.Text) = 0) Then
If vblnShowErrors Then Err.Raise 10000, VALIDATION, "A Default Reference Number must be entered."
blnDataFail = True
End If
Can I simply change chkboxOne to "cboboxOne" by swapping the checkbox for a combobox on the form, and replacing "vbChecked" with True? I'm not sure how similar their functionality is syntax wise.
Thanks

To get the item in a combobox you can examine the listindex to see whats selected (there is no value property)
cboboxOne.AddItem "yes" '//listindex is 0
cboboxOne.AddItem "no" '//listindex is 1
cboboxOne.AddItem "maybe"  '//listindex is 2
...
if (cboboxOne.ListIndex = 0) Then '// yes selected
You can also examine the selected text:
if (cboboxOne.List(cboboxOne.ListIndex) = "yes") Then '// yes selected
You can also test against custom integers using ItemData
cboboxOne.AddItem "yes"
cboboxOne.ItemData(cboboxOne.NewIndex) = 42
cboboxOne.AddItem "no"
cboboxOne.ItemData(cboboxOne.NewIndex) = &HBEEF
...
if (cboboxOne.ItemData(cboboxOne.ListIndex) = 42) Then '// yes selected

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

Is there any way to save the checkbox's state of a program made in visual basic 6.0

I have made(designed) a program in Visual Basic 6.0,it consists of around 100 checkboxes ,the program does not require any code just a yes/no checkbox type program , but I want to save the checkbox state ,so that if a check box is in yes state then after restarting the program it's state remains conserved .
I have read about
My.Settings.Save but I dont know how to use it , I am using Visual Basic 6.0.
Create keys in Registry, save each checkbox value on their checkbox Change event and load the status of each of them in the form Initialize event code.
Option Explicit
Private Const MyApp As String = "My Own App" 'put here your application name
Private Const Sett As String = "Settings"
Private Sub CheckBox1_Change()
Dim chkBoxStatus As String
chkBoxStatus = "CheckBox1"
If Me.CheckBox1.value = True Then
SaveSetting MyApp, Sett, chkBoxStatus, CStr(True)
Else
SaveSetting MyApp, Sett, chkBoxStatus, CStr(False)
End If
End Sub
Do the same for all your check boxes.
And then:
Private Sub UserForm_Initialize() 'I do not remember well if VB6 uses Form_Initialize... You must adapt it accordingly.
Dim regValue As String
regValue = GetSetting(MyApp, Sett, "CheckBox1", "No value")
If regValue <> "No value" Then Me.CheckBox1.value = CBool(regValue)
'do the same for all checkboxes in discussion
'.
'.
End Sub
"No value" is returned if no value has been set in Registry (yet)...
Well, I would have made all checkboxes a control array and would save their sate in a text file in some hidden place, like C:\Users\UserName\AppData\Local. It would be a pretty small code.

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.

VB6 how to get the selected/checked control in a control array

I have to modify a VB6 app and am repeatedly beating my head against a wall over control arrays.
I know that the event handler for the array includes its index value and I can set some variable there, but i should be able to directly access the selected radio button in an array of OptionButton. Currently I'm doing this
For i = 0 To optView.Count - 1
If optView.Item(i).value = True Then
currIndex = i
Exit For
End If
Next
Is this really my only option?
Yes, this is our only option. The control array object does not contain any selecting logic (which makes sense, as "selected" might mean different things for different controls). The only change I'd make is replacing the For with For Each.
Another way to do this that I have used. Write a function, and then call the function, passing in the control name, to return the index number. Then you can reuse this in the future especially, if you add it to a module (.bas).
Function f_GetOptionFromControlArray(opts As Object) As Integer
' From http://support.microsoft.com/KB/147673
' This function can be called like this:
' myVariable = f_GetOptionFromControlArray(optMyButtons) 'Control syntax OK
' myVariable = f_GetOptionFromControlArray(optMyButtons()) 'Array syntax OK
On Error GoTo GetOptionFail
Dim opt As OptionButton
For Each opt In opts
If opt.Value Then
f_GetOptionFromControlArray = opt.Index
Exit Function
End If
Next
GetOptionFail:
f_GetOptionFromControlArray = -1
End Function

List values in AppleScript?

I have the following AppleScript so far:
# List of possible options to control the development environment.
set WhatDoUWantToDoList to {"1", "2", "3", "4"}
set MySites to {"test1", "test2"}
# task selected
set selectedTask to {choose from list WhatDoUWantToDoList with prompt "Pick your task now!!!" without multiple selections allowed}
if selectedTask is equal to {"1"} then
display dialog selectedTask
else
# site selected
set selectedSite to {choose from list MySites with prompt "Pick the site your working on!!!"}
if (selectedTask is not equal to false and selectedSite is not equal to false) then
display dialog selectedTask
display dialog selectedSite
else
display dialog "you messed up!"
end if
end if
I am trying to say if option 1 is selected in list 1 display only the selected task, however, if any other option is selected in list 1 you have to move to the new code block, and must select an option in list 2, if you cancel on list 1 and list 2 you screwed up.
Any ideas on what I am missing here?
{ } in AppleScript creates a list, so when you set selectedTask, you're putting the results from choose from list into another list. When you try to compare the result to {"1"}, it's actually {{"1"}}, so it isn't equal.
Use parentheses ( ) for grouping instead.
using this code worked: if selectedTask contains "1" then
Choose from list will always return an array because multiple selection is possible. The basic idea is:
set selectedValues to (choose from list {"Value 1", "Value 2"} with prompt "Choose")
if (selectedValues is not false) then
set selectedValue to item 1 of selectedValues
display dialog ("You chose " & selectedValue as text)
else
display dialog ("You did not chose!")
end if

Resources