View Full Version : Error......


aks6190
03-02-2011, 12:58 AM
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Dim bid As Integer


Private Sub cboBid_Click()
Dim rs2 As New ADODB.Recordset
bid = Val(cboBid.Text)
rs2.Open "SELECT * FROM Book_info where Book_id= " & bid & " ", con, adOpenKeyset, adLockOptimistic
rs2.MoveFirst


While (rs2.EOF <> True)
If (rs2(0) = bid) Then
txtBookname.Text = (rs2("Bookname"))
txtDis.Text = (rs2("Discount"))
txtSp.Text = (rs2("Sellingprice"))
rs2.MoveNext
End If
Wend
rs2.Close
End Sub



Private Sub cmdAdd_Click()
enablerec
clearrec
MsgBox "You can add new record", vbOKOnly + vbInformation
'txtBillno.Text = (rs("Bill_no")) + 1
txtCName.SetFocus
End Sub

Private Sub cmdCal_Click()
txtTotal.Text = Val(txtSp.Text) * Val(txtQuantity.Text)
End Sub

Private Sub cmdCancel_Click()
clearrec
End Sub

Private Sub cmdExit_Click()
Dim p As String
p = MsgBox(" Do you want to exit? ", vbYesNo + vbInformation)
If p = vbYes Then
Unload Me
frmMdi.Show
Else
frmBill.Show
End If
End Sub

Private Sub cmdSave_Click()
'Dim rs4 As New ADODB.Recordset
'rs1.Open "SELECT * FROM Sales where Book_id=' " & cboBid.Text & " '", con, adOpenDynamic, adLockPessimistic
'Set res5 = New ADODB.Record
rs1.AddNew
rs1("Sales_date") = DTPicker1.Value
rs1("Sales_id") = txtSalesid.Text
rs1("C_name") = txtCName.Text
rs1("Book_id") = cboBid.Text
rs1("Total") = txtTotal.Text
rs1.Update
rs1.Close


'Dim rs3 As New ADODB.Recordset
'rs.Open "SELECT * FROM Book_info where Book_id= " & cboBid.Text & " ", con, adOpenDynamic, adLockPessimistic
'Set res1 = New ADODB.Recordset
rs.AddNew
rs("Book_id") = cboBid.Text
rs("Bookname") = txtBookname.Text
rs("Discount") = txtDis.Text
rs("Sellingprice") = txtSp.Text
rs("Quantity") = txtQuantity.Text
rs.Update
MsgBox "Record is saved", vbOKOnly + vbInformation

rs.Close

End Sub



Private Sub Form_Load()

Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
con.Provider = "Microsoft.Jet.OLEDB.4.0"
con.Open App.Path & "\Bookshop.mdb"

rs.ActiveConnection = con
rs.CursorLocation = adUseClient
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
rs.Open "SELECT * FROM Book_info"


Set rs1 = New ADODB.Recordset
rs1.ActiveConnection = con
rs1.CursorLocation = adUseClient
rs1.CursorType = adOpenDynamic
rs1.LockType = adLockOptimistic
rs1.Open "SELECT * FROM Sales"




End Sub


Private Sub enablerec()
txtSalesid.Locked = False
txtCName.Locked = False
cboBid.Locked = False
txtBookname.Locked = False
txtDis.Locked = False
txtSp.Locked = False
txtQuantity.Locked = False
txtTotal.Locked = False
End Sub

Private Sub clearrec()
txtSalesid.Text = ""
txtCName.Text = ""
cboBid.Text = ""
txtBookname.Text = ""
txtDis.Text = ""
txtSp.Text = ""
txtQuantity.Text = ""
txtTotal.Text = ""
End Sub

i am able to save data into the database but when i press save button it shows errors but the data i wont save is going to the database...............i am a bit confused plz help!.....i am using Vb 6.0 ans Ms access 2007
i am highlighting statements where errors occur.....

Guus2005
03-07-2011, 12:09 PM
first: you should use rs.edit before rs.update and rs.save after rs.addnew. you are using rs.Addnew and rs.Update after each other.
second:
rs1.Open "SELECT * FROM Sales where Book_id=' " & cboBid.Text & " '", con, adOpenDynamic, adLockPessimistic
you might want to consider converting the cboBid.Text to a number using CLng() and you should use the single quote for numbers.
rs1.Open "SELECT * FROM Sales where Book_id= " & CLng(cboBid.Text), con, adOpenDynamic, adLockPessimistic
HTH:D

boblarson
03-07-2011, 12:20 PM
ALSO - do NOT use .TEXT - Get rid of them all in that code. Just use the .Value and you don't need to put .Value because it is the default.

So this:

rs("Book_id") = cboBid.Text
rs("Bookname") = txtBookname.Text
rs("Discount") = txtDis.Text
rs("Sellingprice") = txtSp.Text
rs("Quantity") = txtQuantity.Text

Needs to be this:

rs("Book_id") = cboBid
rs("Bookname") = txtBookname
rs("Discount") = txtDis
rs("Sellingprice") = txtSp
rs("Quantity") = txtQuantity

The use of .Text requires the control to have the focus where using the value does not. There are very few places where you need to use .Text.

boblarson
03-07-2011, 12:22 PM
Oh, I just noticed you said you are using VB6 FE and Access BE. Ignore what I said then. Also you posted this in the wrong category. I'm moving it to the VB category.

aks6190
03-10-2011, 11:50 PM
Thank u Sir!.....Thank u very much!........