I am supposed to create a form with a flexgrid that should display all contents of my database
books.mdb. I also added a button called "Add" which when clicked should display another form with
textboxes where I could fill up the contents I want to add in my database. When I click the button,
this second form should disappear while the main form should still display all contents of the database including the new entry I've added.
Now the problem is the add button doesn't work. I have opened the components and references needed for this project to work. Is there a problem with my code? Please help. Thank you.
=============== =Form1========= ==========
[CODE=vb]Option Explicit
Private rscars As New ADODB.Recordset
Public n1 As String
Public n2 As String
Public n3 As String
Public n4 As String
Public n5 As String
Private Sub form_load()
Dim strqry As String
Call opencars
strqry = "Select * from books"
Set rscars = retrieve(rscars , strqry)
Set MSHFlexGrid1.Da taSource = rscars.DataSour ce
With MSHFlexGrid1
.ColWidth(0) = 1000
.ColWidth(1) = 1500
.ColWidth(2) = 3000
.ColWidth(3) = 2000
.ColWidth(4) = 4000
.ColWidth(5) = 2000
End With
End Sub
Private Sub Command1_Click( )
Dim strqr As String
Form2.Show vbModal
strqr = "insert into books (cat, desc, price, cont, store)" _
& "values('" & n1 & "','" & n2 & "','" & n3 & "','" & n4 & "','" & n5 & "')"
cn.execute strqr
rscars.Requery
Set rscars.DataSour ce = retrieve(rscars , strqr)
Call Display
End Sub
Private Sub Display()
With rscars
If .RecordCount < 1 Then
MsgBox "Table is empty", vbInformation
Exit Sub
Else
n1 = .Fields(0).Valu e & ""
n2 = .Fields(1).Valu e & ""
n3 = .Fields(2).Valu e & ""
n4 = .Fields(3).Valu e & ""
n5 = .Fields(4).Valu e & ""
End If
End With
End Sub[/CODE]
=============== Form2========== =============== =
[code=vb]Private Sub Command1_Click( )
Form1.n1 = catalog.Text
Form1.n2 = description.Tex t
Form1.n3 = price.Text
Form1.n4 = contactp.Text
Form1.n5 = store.Text
Unload Me
End Sub[/code]
=============== ===Module1===== =============== ===== ==
[code=vb]Option Explicit
Public cn As New ADODB.Connectio n
Public rs As New ADODB.Recordset
Public Sub opencars()
With cn
.Provider = "Microsoft.Jet. OLEDB.4.0"
.ConnectionStri ng = App.Path & "\books.mdb "
.Open
End With
End Sub
Public Sub opengas()
With rs
.Source = "Select * from books"
.CursorLocation = adUseClient
.CursorType = adLockOptimisti c
.ActiveConnecti on = cn
.Open
End With
End Sub
Public Function retrieve(ByVal recset As ADODB.Recordset , ByVal qry As String) As ADODB.Recordset
With recset
If recset.State = adStateOpen Then
.Close
End If
.Source = qry
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.LockType = adLockOptimisti c
.ActiveConnecti on = cn
.Open
End With
Set retrieve = recset.DataSour ce
End Function
Public Sub execute(ByVal qry As String)
On Error GoTo Err
With cn
.BeginTrans
.execute qry
.CommitTrans
End With
Err:
cn.RollbackTran s
MsgBox Err.description
End Sub
[/code]
books.mdb. I also added a button called "Add" which when clicked should display another form with
textboxes where I could fill up the contents I want to add in my database. When I click the button,
this second form should disappear while the main form should still display all contents of the database including the new entry I've added.
Now the problem is the add button doesn't work. I have opened the components and references needed for this project to work. Is there a problem with my code? Please help. Thank you.
=============== =Form1========= ==========
[CODE=vb]Option Explicit
Private rscars As New ADODB.Recordset
Public n1 As String
Public n2 As String
Public n3 As String
Public n4 As String
Public n5 As String
Private Sub form_load()
Dim strqry As String
Call opencars
strqry = "Select * from books"
Set rscars = retrieve(rscars , strqry)
Set MSHFlexGrid1.Da taSource = rscars.DataSour ce
With MSHFlexGrid1
.ColWidth(0) = 1000
.ColWidth(1) = 1500
.ColWidth(2) = 3000
.ColWidth(3) = 2000
.ColWidth(4) = 4000
.ColWidth(5) = 2000
End With
End Sub
Private Sub Command1_Click( )
Dim strqr As String
Form2.Show vbModal
strqr = "insert into books (cat, desc, price, cont, store)" _
& "values('" & n1 & "','" & n2 & "','" & n3 & "','" & n4 & "','" & n5 & "')"
cn.execute strqr
rscars.Requery
Set rscars.DataSour ce = retrieve(rscars , strqr)
Call Display
End Sub
Private Sub Display()
With rscars
If .RecordCount < 1 Then
MsgBox "Table is empty", vbInformation
Exit Sub
Else
n1 = .Fields(0).Valu e & ""
n2 = .Fields(1).Valu e & ""
n3 = .Fields(2).Valu e & ""
n4 = .Fields(3).Valu e & ""
n5 = .Fields(4).Valu e & ""
End If
End With
End Sub[/CODE]
=============== Form2========== =============== =
[code=vb]Private Sub Command1_Click( )
Form1.n1 = catalog.Text
Form1.n2 = description.Tex t
Form1.n3 = price.Text
Form1.n4 = contactp.Text
Form1.n5 = store.Text
Unload Me
End Sub[/code]
=============== ===Module1===== =============== ===== ==
[code=vb]Option Explicit
Public cn As New ADODB.Connectio n
Public rs As New ADODB.Recordset
Public Sub opencars()
With cn
.Provider = "Microsoft.Jet. OLEDB.4.0"
.ConnectionStri ng = App.Path & "\books.mdb "
.Open
End With
End Sub
Public Sub opengas()
With rs
.Source = "Select * from books"
.CursorLocation = adUseClient
.CursorType = adLockOptimisti c
.ActiveConnecti on = cn
.Open
End With
End Sub
Public Function retrieve(ByVal recset As ADODB.Recordset , ByVal qry As String) As ADODB.Recordset
With recset
If recset.State = adStateOpen Then
.Close
End If
.Source = qry
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.LockType = adLockOptimisti c
.ActiveConnecti on = cn
.Open
End With
Set retrieve = recset.DataSour ce
End Function
Public Sub execute(ByVal qry As String)
On Error GoTo Err
With cn
.BeginTrans
.execute qry
.CommitTrans
End With
Err:
cn.RollbackTran s
MsgBox Err.description
End Sub
[/code]
Comment