OK I see you are using the ADO data control...not the data environment
The With Statement basically initializes the object you are using and also helps minimize typing....for instance if you want to move to the next record instead of typing
datTingleLine.Recordset.MoveNext ...You could have
With datTingleLine.Recordset
.MoveNext
End With
This is particulary useful if you have lots of lines of code
For your case you dont need to initialize upto....datTingleLine.Recordset.Command1...
Instead you only need to initialize upto
datTingleLine.Recordset to be able to access the ADO Data control's methods (eg .MoveNext is a method) and properties (eg .RecordCount is a property) by preceeding them with a period (.)
The Sub GrandTotal() that I included is only a sub-routine or sub program that you would write in the declarations section of your form (...General....).You can then call the sub-routine in your Form_Initialize() event eg
Private Sub Form_Initialize()
Call GrandTotal
End Sub
or alternatively
Private Sub Form_Initialize()
GrandTotal
End Sub
So in your case you could have the following code in your Form_Initialize() event
i.e.
Private Sub Form_Initialize()
Dim I As Integer
Dim Increment As Long
Dim Quantity As Integer
Dim Cost As Currency
Dim X As Long
With datTingleLine.Recordset
If .RecordCount= 0 then
Exit Sub
Else
.MoveFirst
For I = 1 to .RecordCount
Quantity=.Fields("Quantity") 'Quantity field
Cost=.Fields("Cost") 'Cost field
X= Quantity * Cost 'The calculation
Increment= Increment + X 'Icrement the total
.MoveNext 'To move to the next record
If .EOF Then 'End Of File
txtGrandTotal.Text=Increment 'The textbox
Exit For
End If
Next I
End If
End with
End sub
(This would execute slower if you had lots of lines of code in the Form_Initialize() event)
Or You can write the GrandTotal sub-routine in the general section of your form code module
eg
Sub GrandTotal()
Dim I As Integer
Dim Increment As Long
Dim Quantity As Integer
Dim Cost As Currency
Dim X As Long
With datTingleLine.Recordset
If .RecordCount= 0 then
Exit Sub
Else
.MoveFirst
For I = 1 to .RecordCount
Quantity=.Fields("Quantity") 'Quantity field
Cost=.Fields("Cost") 'Cost field
X= Quantity * Cost 'The calculation
Increment= Increment + X 'Icrement the total
.MoveNext 'To move to the next record
If .EOF Then 'End Of File
txtGrandTotal.Text=Increment 'The textbox
Exit For
End If
Next I
End If
End with
End sub
and then call the sub-program as follows
Private Sub Form_initialize()
GrandTotal
End Sub
(This is much faster)
Hope this works for you...Cheers!!!