A
Ann
Hi,
I found an exercise online on how to create a 2002 Word user form that has
text boxes that I can type into. The data typed into the text boxes ends up
in an Access database table on the click of a button. Now I would like to
add a combo box. I created the box and can get the items to show in my list
but I'm just not sure how to get the item I choose into the database table.
I'm not a programmer but can use some of what I find on the internet and
tweak it for my needs. If I could just get this to be able to use combo
boxes and check boxes it's a tool I can used repeatedly in my job. Can
anyone help me? I really do appreciate any help I can get...thanks in
advance.
Here is the code I have so far:
Private Sub UserForm_Initialize()
cmbChoose.AddItem "Choice One"
cmbChoose.AddItem "Choice Two"
End Sub
Private Sub cmdAlreadyEntered_Click()
'declare variables for new connection and recordset and declare variables
Dim vConnection As New ADODB.Connection
Dim vRecordSet As New ADODB.Recordset
Dim vClientFName As String
Dim vClientLName As String
Dim vCompany As String
Dim vAddress As String
Dim vCity As String
Dim vState As String
Dim vZip As String
Dim vPhone As String
Dim vNotes As String
'provide connection string for data using Jet Provider for Access database
vConnection.ConnectionString = "data
source=\\corpfs03\Shared\FACILADM\SHARED\OIPC\Users\Ann\Goals\WordClientInfo.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
'open connection
vConnection.Open
'test connection state...this can later be commented out...but it helps
'let you know if the connection was successful when testing the code
vConnectionState = vConnection.State
If vConnectionState = 1 Then
MsgBox "The connection to this database is working!", vbInformation
Else
MsgBox "You were unable to connect to the assigned database!",
vbInformation
End If
'set variable equal to whatever is entered into First & LastName form
field
'so you can query the DB with this name to see if a record exists
vClientFName = txtFirstName.Text
vClientLName = txtLastName.Text
'open a RecordSet with SQL query results...to see if first/last name
matches a record
vRecordSet.Open "SELECT * FROM tblClientInfo WHERE
tblClientInfo!txtFirstName = " & _
Chr(34) & vClientFName & Chr(34) & "AND tblClientInfo!txtLastName = "
& _
Chr(34) & vClientLName & Chr(34), vConnection, adOpenKeyset,
adLockOptimistic
'if a match is found, display it to the user in a message box
'you'll get a match if you are NOT .EOF (end of file)
With vRecordSet
If Not .EOF Then
vCorrectRecord = MsgBox("Is this the correct record?" & Chr(13) &
Chr(13) & _
vRecordSet("txtFirstName") & " " & _
vRecordSet("txtLastName") & ", " & _
vRecordSet("txtAddress") & ", " & _
vRecordSet("txtCity"), _
vbYesNo + vbInformation, "User Record")
Else
'if you ARE *else* (are .EOF), that means no record was matched,
tell the user
MsgBox "No possible match was found."
End If
End With
'If the answer equals 6, it means they clicked Yes on the MsgBox
accepting this match
If vCorrectRecord = 6 Then
'if yes...set variables from DB fields (the name variables are
already set above)
vCompany = vRecordSet("txtCompany")
vAddress = vRecordSet("txtAddress")
vCity = vRecordSet("txtCity")
vState = vRecordSet("txtState")
vZip = vRecordSet("txtZip")
vPhone = vRecordSet("txtPhone")
vNotes = vRecordSet("memNotes")
'set the form field's bookmarks to the results from the database
field variables
txtCompany.Text = vCompany
txtAddress.Text = vAddress
txtCity.Text = vCity
txtState.Text = vState
txtZip.Text = vZip
txtPhone.Text = vPhone
txtNotes.Text = vNotes
Else
'if not 6, then not Yes, so must be NO...remind user to update
database!
MsgBox "Since this is not the correct entry..." & Chr(13) & _
"be sure to fill out remaining form fields and click *Update* " &
Chr(13) & _
"so this person will be added to the database."
End If
'close objects
vRecordSet.Close
vConnection.Close
'clear object to free up memory
Set vRecordSet = Nothing
Set vConnection = Nothing
End Sub
Private Sub cmdUpdateNew_Click()
'declare new connection, recordset and variables
Dim vConnection As New ADODB.Connection
Dim vRecordSet As New ADODB.Recordset
Dim vClientFName As String
Dim vClientLName As String
Dim vCompany As String
Dim vAddress As String
Dim vCity As String
Dim vState As String
Dim vZip As String
Dim vPhone As String
Dim vNotes As String
'provide same as previous connection string for data using Jet Provider
for Access database
vConnection.ConnectionString = "data
source=\\corpfs03\Shared\FACILADM\SHARED\OIPC\Users\Ann\Goals\WordClientInfo.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
'open connection
vConnection.Open
'set variables from text enter into all the AutoForm field bookmark
results
vClientFName = txtFirstName.Text
vClientLName = txtLastName.Text
vCompany = txtCompany.Text
vAddress = txtAddress.Text
vCity = txtCity.Text
vState = txtState.Text
vZip = txtZip.Text
vPhone = txtPhone.Text
vNotes = txtNotes.Text
'Open a new version of the temporary RecordSet accessing the ClientInfo
table in Database
vRecordSet.Open "tblClientInfo", vConnection, adOpenKeyset,
adLockOptimistic
vRecordSet.AddNew
'we can't enter "nothing", so only set variables with some data entered
info the field
If vClientFName <> "" Then vRecordSet!txtFirstName = vClientFName
If vClientLName <> "" Then vRecordSet!txtLastName = vClientLName
If vCompany <> "" Then vRecordSet!txtCompany = vCompany
If vAddress <> "" Then vRecordSet!txtAddress = vAddress
If vCity <> "" Then vRecordSet!txtCity = vCity
If vState <> "" Then vRecordSet!txtState = vState
If vZip <> "" Then vRecordSet!txtZip = vZip
If vPhone <> "" Then vRecordSet!txtPhone = vPhone
If vNotes <> "" Then vRecordSet!memNotes = vNotes
'update the RecordSet to the database, this adds your new client to the DB
vRecordSet.Update
'advise the user the client has been added
MsgBox vClientFName & " " & vClientLName & " has been added to your
database."
'close objects
vRecordSet.Close
vConnection.Close
'clear object to free up memory
Set vRecordSet = Nothing
Set vConnection = Nothing
End Sub
I found an exercise online on how to create a 2002 Word user form that has
text boxes that I can type into. The data typed into the text boxes ends up
in an Access database table on the click of a button. Now I would like to
add a combo box. I created the box and can get the items to show in my list
but I'm just not sure how to get the item I choose into the database table.
I'm not a programmer but can use some of what I find on the internet and
tweak it for my needs. If I could just get this to be able to use combo
boxes and check boxes it's a tool I can used repeatedly in my job. Can
anyone help me? I really do appreciate any help I can get...thanks in
advance.
Here is the code I have so far:
Private Sub UserForm_Initialize()
cmbChoose.AddItem "Choice One"
cmbChoose.AddItem "Choice Two"
End Sub
Private Sub cmdAlreadyEntered_Click()
'declare variables for new connection and recordset and declare variables
Dim vConnection As New ADODB.Connection
Dim vRecordSet As New ADODB.Recordset
Dim vClientFName As String
Dim vClientLName As String
Dim vCompany As String
Dim vAddress As String
Dim vCity As String
Dim vState As String
Dim vZip As String
Dim vPhone As String
Dim vNotes As String
'provide connection string for data using Jet Provider for Access database
vConnection.ConnectionString = "data
source=\\corpfs03\Shared\FACILADM\SHARED\OIPC\Users\Ann\Goals\WordClientInfo.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
'open connection
vConnection.Open
'test connection state...this can later be commented out...but it helps
'let you know if the connection was successful when testing the code
vConnectionState = vConnection.State
If vConnectionState = 1 Then
MsgBox "The connection to this database is working!", vbInformation
Else
MsgBox "You were unable to connect to the assigned database!",
vbInformation
End If
'set variable equal to whatever is entered into First & LastName form
field
'so you can query the DB with this name to see if a record exists
vClientFName = txtFirstName.Text
vClientLName = txtLastName.Text
'open a RecordSet with SQL query results...to see if first/last name
matches a record
vRecordSet.Open "SELECT * FROM tblClientInfo WHERE
tblClientInfo!txtFirstName = " & _
Chr(34) & vClientFName & Chr(34) & "AND tblClientInfo!txtLastName = "
& _
Chr(34) & vClientLName & Chr(34), vConnection, adOpenKeyset,
adLockOptimistic
'if a match is found, display it to the user in a message box
'you'll get a match if you are NOT .EOF (end of file)
With vRecordSet
If Not .EOF Then
vCorrectRecord = MsgBox("Is this the correct record?" & Chr(13) &
Chr(13) & _
vRecordSet("txtFirstName") & " " & _
vRecordSet("txtLastName") & ", " & _
vRecordSet("txtAddress") & ", " & _
vRecordSet("txtCity"), _
vbYesNo + vbInformation, "User Record")
Else
'if you ARE *else* (are .EOF), that means no record was matched,
tell the user
MsgBox "No possible match was found."
End If
End With
'If the answer equals 6, it means they clicked Yes on the MsgBox
accepting this match
If vCorrectRecord = 6 Then
'if yes...set variables from DB fields (the name variables are
already set above)
vCompany = vRecordSet("txtCompany")
vAddress = vRecordSet("txtAddress")
vCity = vRecordSet("txtCity")
vState = vRecordSet("txtState")
vZip = vRecordSet("txtZip")
vPhone = vRecordSet("txtPhone")
vNotes = vRecordSet("memNotes")
'set the form field's bookmarks to the results from the database
field variables
txtCompany.Text = vCompany
txtAddress.Text = vAddress
txtCity.Text = vCity
txtState.Text = vState
txtZip.Text = vZip
txtPhone.Text = vPhone
txtNotes.Text = vNotes
Else
'if not 6, then not Yes, so must be NO...remind user to update
database!
MsgBox "Since this is not the correct entry..." & Chr(13) & _
"be sure to fill out remaining form fields and click *Update* " &
Chr(13) & _
"so this person will be added to the database."
End If
'close objects
vRecordSet.Close
vConnection.Close
'clear object to free up memory
Set vRecordSet = Nothing
Set vConnection = Nothing
End Sub
Private Sub cmdUpdateNew_Click()
'declare new connection, recordset and variables
Dim vConnection As New ADODB.Connection
Dim vRecordSet As New ADODB.Recordset
Dim vClientFName As String
Dim vClientLName As String
Dim vCompany As String
Dim vAddress As String
Dim vCity As String
Dim vState As String
Dim vZip As String
Dim vPhone As String
Dim vNotes As String
'provide same as previous connection string for data using Jet Provider
for Access database
vConnection.ConnectionString = "data
source=\\corpfs03\Shared\FACILADM\SHARED\OIPC\Users\Ann\Goals\WordClientInfo.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
'open connection
vConnection.Open
'set variables from text enter into all the AutoForm field bookmark
results
vClientFName = txtFirstName.Text
vClientLName = txtLastName.Text
vCompany = txtCompany.Text
vAddress = txtAddress.Text
vCity = txtCity.Text
vState = txtState.Text
vZip = txtZip.Text
vPhone = txtPhone.Text
vNotes = txtNotes.Text
'Open a new version of the temporary RecordSet accessing the ClientInfo
table in Database
vRecordSet.Open "tblClientInfo", vConnection, adOpenKeyset,
adLockOptimistic
vRecordSet.AddNew
'we can't enter "nothing", so only set variables with some data entered
info the field
If vClientFName <> "" Then vRecordSet!txtFirstName = vClientFName
If vClientLName <> "" Then vRecordSet!txtLastName = vClientLName
If vCompany <> "" Then vRecordSet!txtCompany = vCompany
If vAddress <> "" Then vRecordSet!txtAddress = vAddress
If vCity <> "" Then vRecordSet!txtCity = vCity
If vState <> "" Then vRecordSet!txtState = vState
If vZip <> "" Then vRecordSet!txtZip = vZip
If vPhone <> "" Then vRecordSet!txtPhone = vPhone
If vNotes <> "" Then vRecordSet!memNotes = vNotes
'update the RecordSet to the database, this adds your new client to the DB
vRecordSet.Update
'advise the user the client has been added
MsgBox vClientFName & " " & vClientLName & " has been added to your
database."
'close objects
vRecordSet.Close
vConnection.Close
'clear object to free up memory
Set vRecordSet = Nothing
Set vConnection = Nothing
End Sub