Expected Function or variable vb6.0 - vb6

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

Related

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.

Is there a built in MS Access VBA Event to simplify Data Verification and SetFocus

Bit of a rookie issue here. How do you deal with data verification using Access Events? The problem is that when I use SetFocus to return the Cursor to the field with the errant data, Access goes through the _Exit and/or _LostFocus Events of the next Control in the Tab Order. If those include data validation procedures, the desired SetFocus is circumvented.
Using techturtles answer, I came up with this "solution" (read "hack").
Private Sub ServicingEmployee_LostFocus() 'Check data validity
If dataEntryCancelled Then Exit Sub
Dim cntrl As Control
Set cntrl = Me.ServicingEmployee
Dim myResponse As Integer
If IsNull(cntrl.Value) Then
myResponse = MsgBox("This field cannot be left blank.", vbOKOnly, "Enter Date Collected")
dataErrorInPreviousField = True
**'Next line sets a Public Control**
Set controlWithDataEntryError = Me.ServicingEmployee
End If
End Sub
Private Sub Client_GotFocus() '**Check for data Error in previous Control according to Tab Order**
If dataErrorInPreviousField Then Call dataErrorProc
End Sub
Public Sub dataErrorProc()
With controlWithDataEntryError
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
dataErrorInPreviousField = False
End Sub
Private Sub Client_Exit(Cancel As Integer) '**Example of Bypassing _Exit Event**
If dataEntryCancelled Or dataErrorInPreviousField Then Exit Sub
.
...
End Sub
My question is this: Is there a simpler way to accomplish this?
First, I would strongly encourage you to add table constraints in addition to your form validation, if you haven't already done so. Assuming this form is tied to a table in Access, just make sure the ServicingEmployee field (in the table) is set to required. That way, you have another level of validation making sure the field has a value.
For form validation, you can use either the form's or control's BeforeUpdate event. If you use the form's event, you only have to run the check once.
I would also suggest having a separate function (within the form) to check validity. Something like this (here, I'm using a form tied to an Employees table, with a first name and last field which should both be non-null):
Private Function isFormValid() As Boolean
' we start off assuming form is valid
isFormValid = True
If IsNull(Me.txtFirstName) Then
MsgBox "First name cannot be blank", vbOKOnly
Me.txtFirstName.SetFocus
isFormValid = False
ElseIf IsNull(Me.txtLastName) Then
MsgBox "Last name cannot be blank", vbOKOnly
Me.txtLastName.SetFocus
isFormValid = False
End If
End Function
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not isFormValid Then
Cancel = 1
End If
End Sub

How to pass value to SQL Query in Data Environment's Command Object at run-time in Visual Basic 6.0?

In DataEnvironment I have a Command Object in which I have
given following SQL Query:
SELECT * FROM userdetails WHERE Date = Todays_date
Here Todays_date is a public variable in Module.
This variable accepts value at run-time.
How to call variable in DataEnvironment's SQL Query?
The VB6 manuals cover this in Closing and Reopening the Recordset under the heading Data Environment Programming Guidelines. The example given there looks like:
Option Explicit
Private Sub Command1_Click()
' You must close the recordset before changing the parameter.
If DataEnvironment1.rsCommandQuery.State = adStateOpen Then
DataEnvironment1.rsCommandQuery.Close
End If
' Reopen the recordset with the input parameter supplied by
' the TextBox control.
DataEnvironment1.CommandQuery Text1.Text
With Text2
.DataField = "AU_LName"
.DataMember = "CommandQuery"
Set .DataSource = DataEnvironment1
End With
End Sub
Private Sub Form_Load()
' Supply a default value.
Text1.Text = "172-32-1172"
' Change the CommandButton caption.
Command1.Caption = "Run Query"
End Sub
You call the Command object as a method of its parent DataEnvironment, passing the parameters there. Gotta love The Fine Manual.

Forms(frmName) NOT WORKING

I am trying to use a string variable instead of hardcoding the forms name as below.
FrmMainControl.Controls
but want to use this.
Forms("FrmMainControl").Controls
However, i get the error Sub or Function not defined. Please help. This is in VBA btw.
Assuming the form is loaded you can search the UserForms collection;
Dim form As UserForm: Set form = getInstanceOfLoadedUfrm("frmMainControl")
If Not form Is Nothing Then
MsgBox form.Controls.Count
Else
'//load
Set form = UserForms.Add("frmMainControl")
MsgBox form.Controls.Count
End If
Function getInstanceOfLoadedUfrm(name As String) As UserForm
name = UCase$(name)
For Each form In UserForms
If (Ucase$(form.name) = name) Then
Set getInstanceOfLoadedUfrm = form
End If
Next
End Function

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

Resources