Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding Code 1

Status
Not open for further replies.

nubianqueen

Technical User
Apr 16, 2002
19
US
I have a piece of code(I didn't write)that I think may work in a combo box I have on a form. Users would select a policy title from the combo box that they want to enter information associataed with title. That information is in a subform. I'm not that good w/VBA. If someone could briefly tell me what this code will do I will greatly appreciate it.

Private Sub cmbPolicyTitle_AfterUpdate()

Dim db As Database, rs As Recordset
Dim tSQL As String, tmp As Boolean

Set db = CurrentDb
Set rst = db.OpenRecordset("qryApproveHead")

If IsNull(Me![cmbPolicyTitle].Text) Then
txtPolicyNum = " "
rst.Close
Else
'find the record that matches the control.
rst.FindFirst "[PolicyTitle] = '" & PolicyTitle "'"
txtPolicyNum = rst!PolicyNum

'find the record that matches the control.
Me.RecordsetClone.FindFirst "[PolicyNum] = " & txtPolicyNum
Me.Bookmark = Me.RecordsetClone.Bookmark
tmp = Me.RecordsetClone.NoMatch
rst.Close
End If

If tmp Then
DoCmd.GoToRecord , , acNewRec
End If
cmbPolicyTitle.SetFocus


End Sub
If more info is needed please let me know. Thanks in advance.
nubianqueen
 
Private Sub cmbPolicyTitle_AfterUpdate()
'do this after the field cmbPolicyTitle is updated

'create DAO objects to access and manipulate data
Dim db As Database, rs As Recordset

'Create a string variable (tsql) to hold text data, and a true/false variable (tmp)

Dim tSQL As String, tmp As Boolean

'initialize the data objects, the database will be the currently open one
Set db = CurrentDb

'the recordsource will be a query in the database
'called qryApproveHead.

Set rst = db.OpenRecordset("qryApproveHead")

'if the combo is empty, fill a something called
'txtPolicyNum with a space
'txtPolicyNum was not declared in a Dim statement
'so who knows what it is, probably a public variable
'or Options Explicit is not set

If IsNull(Me![cmbPolicyTitle].Text) Then

txtPolicyNum = " "
rst.Close
'if its blank close the recordset, if not
'process these instructions:

Else

'if the combo isn't blank then
'

'find the record that matches the control.


'this is a total mystery since PolicyTitle, like
'txtPolicyNum has appeared from nowhere
'looks like an error

rst.FindFirst "[PolicyTitle] = '" &
PolicyTitle "'"

'this is bad code because it is telling
'the program to do something
'with no provision on how to act if a match is not found

txtPolicyNum = rst!PolicyNum




Me.RecordsetClone.FindFirst "[PolicyNum] = " & txtPolicyNum

'book mark the record if its found
'probably results in the record being
'presented to the user
Me.Bookmark = Me.RecordsetClone.Bookmark

'set tmp to true if no match is found

tmp = Me.RecordsetClone.NoMatch
rst.Close

End If

'if there is no match present the user with a blank
'record
If tmp Then
DoCmd.GoToRecord , , acNewRec
End If

'set the cursor to the combo
cmbPolicyTitle.SetFocus


End Sub
 
> 'if the combo is empty, fill a something called
> 'txtPolicyNum with a space
> 'txtPolicyNum was not declared in a Dim statement
> 'so who knows what it is, probably a public variable
> 'or Options Explicit is not set

I think txtPolicyNum is a textbox on the form.
 

Re: esquared: txtPolicyNum is a textbox on the form. I didn't realize until you posted that that you could get away without qualifying the form controls. I tried it with both explicit on and off, and you're right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top