Although I don't understand how your form and code are working, one thing
I've noticed is that it seems you should be using a parameter query to
create invoices. Here's a possible revision to your code, with explanation.
(I am using Access 2002.)
Regards
Geoff
Private Sub cmdInvoice_Click()
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strSQL As String
DoCmd.RunCommand acCmdSaveRecord
'Make sure they have at least one workorder selected
' I'm not sure what the zfGetDB() function is:
'Set db = zfGetDB()
' So instead:
Set db = CurrentDb()
' Your temporary query was:
'Set qd = db.CreateQueryDef("", "SELECT wrkky_invno FROM " _
& "qryWorkOrders_uninvoiced WHERE wrkky_invno = true;")
' However, I don't understand the next two lines:
'qd![Forms!frmInvoiceSelect!ChooseCust] = Me!ChooseCust
'qd![Forms!frmInvoiceSelect!ChooseJob] = Me!ChooseJob
' Therefore, I've made the following assumption and
' re-written the query. I assume you're trying to restrict
' the records returned by the query in three ways:
' 1. Work Orders that are uninvoiced...
' 2. ...for the customer currently in the form...
' 3. ...for the job currently in the form.
' If that assumption is correct, then it seems you need a
' parameter query, which allows you to set the customer
' and job numbers in this code.
' I've assumed that your query needs the following parameters:
' Parameter 1 = Customer_ID number (long integer).
' Parameter 2 = Job_ID number (long integer).
' The parameters are defined in the parameters clause
' at the beginning of the SQL statement.
' The parameters are also used in the WHERE clause.
' The parameters are given values later in this code.
'
' SQL string for parameter query:
strSQL = "PARAMETERS [Customer_ID] Long, [Job_ID] Long;" _
& vbNewLine _
& "SELECT qryWorkOrders_uninvoiced.wrkky_invno" _
& "& vbNewLine" _
& "FROM qryWorkOrders_uninvoiced" & vbNewLine _
& "WHERE (((qryWorkOrders_uninvoiced.wrkky_invno)" _
& "=True) " _
& "AND ((qryWorkOrders_uninvoiced.Customer_ID)" _
& "=[Customer_ID]) " _
& "AND ((qryWorkOrders_uninvoiced.Job_ID)=[Job_ID]));"
' Incidentally, if the above looks complicated, you can easily
' create the SQL statement for a parameter query at the
' user-interface:
' 1. Open the query in design view.
' 2. Enter a criterion using square brackets, eg the
' criterion for the Customer_ID field would be [Customer_ID].
' 3. Open the Query menu, select Parameters and in the
' parameters dialog, enter [Customer_ID] in column 1 and Long
' in column 2.
' 4. Use the View menu to switch to SQL view.
' 5. Copy the SQL statement and paste it in here. The paste
' operation can be simplified by using the VBA String Editor
' Add-In (on the Add-Ins menu).
' Create temporary query:
Set qd = db.CreateQueryDef("", strSQL)
' Give the parameters values from this form:
qd.Parameters![Customer_ID] = CLng(Me.ChooseCust)
qd.Parameters![Job_ID] = CLng(Me.ChooseJob)
' Open recordset:
Set rs = qd.OpenRecordset(dbOpenSnapshot)
' Before testing the RecordCount property, you should move
' to the last record to ensure you get an accurate record
' count. However, this might be slow with large recordsets,
' so instead, you can examine the BOF and EOF properties
' (see below).
'If rs.RecordCount = 0 Then
' If the Beginning-of-File and End-of-File properties
' are both TRUE, then the recordset contains no records:
If rs.BOF And rs.EOF Then
MsgBox "You have not selected any Work Orders to invoice.", _
vbOKOnly, "No Selection"
Else
DoCmd.OpenForm "frmInvoice", , , , , , Me!ChooseJob
End If
If Not rs Is Nothing Then rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
doglover said:
This is really strange sometimes the form and the text boxes synchronize
ok
and other times they do not. Here is the code behind the form:
Option Compare Database 'Use database order for string comparisons
Option Explicit
Dim X As Integer
Private Sub ChooseCust_AfterUpdate()
On Error Resume Next
Me!ChooseJob = Null
Me!ChooseJob.Requery
Me.Requery
End Sub
Private Sub ChooseJob_AfterUpdate()
On Error Resume Next
Me.FilterOn = False
Me.Requery
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Private Sub cmdInvoice_Click()
DoCmd.RunCommand acCmdSaveRecord
'Make sure they have at least one workorder selected
Dim db As Database
Dim qd As QueryDef
Dim rs As Recordset
Set db = zfGetDB()
Set qd = db.CreateQueryDef("", "SELECT wrkky_invno FROM
qryWorkOrders_uninvoiced WHERE wrkky_invno = true;")
qd![Forms!frmInvoiceSelect!ChooseCust] = Me!ChooseCust
qd![Forms!frmInvoiceSelect!ChooseJob] = Me!ChooseJob
Set rs = qd.OpenRecordset(dbOpenSnapshot)
If rs.RecordCount = 0 Then
MsgBox "You have not selected any Work Orders to invoice.", vbOKOnly,
"No Selection"
Else
DoCmd.OpenForm "frmInvoice", , , , , , Me!ChooseJob
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
Private Sub cmdPrebilling_Click()
Dim strCriteria As String
strCriteria = "jobky_cusnm = '" & Me!ChooseCust & "'" & (" AND jobid = '"
+
Me!ChooseJob + "'")
DoCmd.OpenReport "rptPreBilling", acViewPreview, , strCriteria
End Sub
Private Sub cmdSelect_Click()
Dim rs As Recordset
Set rs = Me.RecordsetClone
With rs
.MoveFirst
Do Until .EOF
.Edit
!wrkky_invno = Not !wrkky_invno
.Update
.MoveNext
Loop
End With
rs.Close
Set rs = Nothing
End Sub
Private Sub Form_Close()
'Remove any wrkky_invno with a true in them before closing
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryWorkOrderDeSelect_qup"
DoCmd.SetWarnings True
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.Filter = "[wrkid]=0"
Me.FilterOn = True
End Sub
Private Sub wrkid_DblClick(Cancel As Integer)
DoCmd.OpenForm "frmWorkOrder", , , , , , "wrk" & wrkid
End Sub
I look forward to hearing from someone that can help.
Thanks,
Doglover
doglover said:
I am converting from access 97 by importing tables, queries, forms,
reports,
macro, modules and this process is complete. I am troubleshooting one
form
that uses queries, code, etc to create an invoice. I am having trouble
synchronizing the form's query resource with text boxes and labels. The
following code in a label "On Click"
=zfSortLabel("tktno",[Screen].[ActiveForm]). I type the code in on the
"on
Click" event and the form text boxes synchronizes fine but if I
completely
close the form and reopen then the brackets around Screen have gone and
the
form does not synchronize. Obviously, it will not work with out the [].
Did
something change from Access 97 to Access 2003? Thanks for your help.
Doglover