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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

auto updating a txtbox with the total from datagrid 1

Status
Not open for further replies.

Tailgun

Technical User
Mar 30, 2002
417
US
I have a datagrid that the user can edit, delete. add etc and it works fine.

The total from one currency column needs to be automaticly updated after the user makes changes and he clicks a Save cmdbutton. At the moment with this code I can only make it work by using a seperate command button to update the total. I would like to do it without using another cmdbutton to update the total. The error I get if I include the code anywhere else is you cannot add a empty row. Hopefully someone can show me how to modifiy this code.



visual basic code:
If Adodc2.Recordset.RecordCount <> 0 And Adodc2.Recordset.BOF = False And Adodc2.Recordset.EOF = False Then
Dim fmtCurr As New StdDataFormat
fmtCurr.Format = &quot;Currency&quot;
Set DataGrid1.Columns(1).DataFormat = fmtCurr
Dim rs As ADODB.Recordset
Dim dSum As Double
Set rs = Adodc2.Recordset.Clone


rs.MoveFirst
Do Until rs.EOF
If IsNumeric(rs(&quot;ValueChgOrder&quot;)) Then _
dSum = dSum + rs(&quot;ValueChgOrder&quot;)
rs.MoveNext
Loop

Set rs = Nothing

txtTotal.Text = FormatCurrency(dSum)
Else
txtTotal.Text = &quot;&quot;

End If


Thanks for any and all help
 
OK, I can't even begin to tell you how bad the structure of your code is.

However, can you tell me why you have an &quot;ELSE&quot; in between the two txtbox functions? Where is its &quot;IF&quot; and &quot;END IF&quot;?

Could you explain the code more clearly?

Thank you,


jgjge3.gif
[tt]&quot;Very funny, Scotty... Now Beam down my clothes.&quot;[/tt]
 
well being somewhat new to this I thought that

If XXXXXXXXX then
do XXXXXXXXXXXXXX
Else
this XXXXXXXXXXXX
End If

was a proper structure.

As I said the code works fine as an independent thing. IE using an update total cmdbutton with this code to update that columns total after changes are made to the datagrid. But I am hoping to do it without that extra cmdbutton and as part of the code when the datagrid changes are saved.
 
If I do an Else If the recordset BOF and EOF = True not coded that way of course.

Then it still opens a new blank row in the database if I put the code anywhere but a seperate cmdbutton.
 
As Jag pointed out, your housekeeping is dreadful.

Try this

Dim fmtCurr As New StdDataFormat
Dim rs As ADODB.Recordset
Dim dSum As Double

fmtCurr.Format = &quot;Currency&quot;
Set DataGrid1.Columns(1).DataFormat = fmtCurr

If Adodc2.Recordset.BOF = False And Adodc2.Recordset.EOF = False Then
Set rs = Adodc2.Recordset.Clone
rs.MoveFirst

Do Until rs.EOF
If IsNumeric(rs(&quot;ValueChgOrder&quot;)) AND rs(&quot;ValueChgOrder&quot;)<>&quot;&quot; Then _
dSum = dSum + rs(&quot;ValueChgOrder&quot;)
rs.MoveNext
Loop

Set rs = Nothing

txtTotal.Text = FormatCurrency(dSum)
Else
txtTotal.Text = &quot;&quot;
End If

BB
 
Thanks to Jag14 for pointing out my bad code and especially thanks to BiggerBrother for setting me straight. I really appreciate the help.

I figured out my other situation I was just placing the code in the wrong place in the datagrid. It now works fine. thanks again to both of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top