473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Visual Basic 6 help--add button and flexgrid

4
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===================
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private rscars As New ADODB.Recordset
  4.  
  5. Public n1 As String
  6. Public n2 As String
  7. Public n3 As String
  8. Public n4 As String
  9. Public n5 As String
  10.  
  11. Private Sub form_load()
  12. Dim strqry As String
  13.  
  14. Call opencars
  15.  
  16. strqry = "Select * from books"
  17. Set rscars = retrieve(rscars, strqry)
  18. Set MSHFlexGrid1.DataSource = rscars.DataSource
  19. With MSHFlexGrid1
  20. .ColWidth(0) = 1000
  21. .ColWidth(1) = 1500
  22. .ColWidth(2) = 3000
  23. .ColWidth(3) = 2000
  24. .ColWidth(4) = 4000
  25. .ColWidth(5) = 2000
  26. End With
  27.  
  28.  
  29. End Sub
  30.  
  31. Private Sub Command1_Click()
  32. Dim strqr As String
  33. Form2.Show vbModal
  34.  
  35. strqr = "insert into books (cat, desc, price, cont, store)" _
  36. & "values('" & n1 & "','" & n2 & "','" & n3 & "','" & n4 & "','" & n5 & "')"
  37. cn.execute strqr
  38. rscars.Requery
  39. Set rscars.DataSource = retrieve(rscars, strqr)
  40. Call Display
  41. End Sub
  42.  
  43. Private Sub Display()
  44. With rscars
  45. If .RecordCount < 1 Then
  46. MsgBox "Table is empty", vbInformation
  47. Exit Sub
  48. Else
  49. n1 = .Fields(0).Value & ""
  50. n2 = .Fields(1).Value & ""
  51. n3 = .Fields(2).Value & ""
  52. n4 = .Fields(3).Value & ""
  53. n5 = .Fields(4).Value & ""
  54. End If
  55. End With
  56. End Sub

===============Form2==========================
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Form1.n1 = catalog.Text
  3. Form1.n2 = description.Text
  4. Form1.n3 = price.Text
  5. Form1.n4 = contactp.Text
  6. Form1.n5 = store.Text
  7. Unload Me
  8. End Sub
==================Module1========================= ==
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public cn As New ADODB.Connection
  4. Public rs As New ADODB.Recordset
  5.  
  6. Public Sub opencars()
  7. With cn
  8. .Provider = "Microsoft.Jet.OLEDB.4.0"
  9. .ConnectionString = App.Path & "\books.mdb"
  10. .Open
  11. End With
  12. End Sub
  13.  
  14. Public Sub opengas()
  15. With rs
  16. .Source = "Select * from books"
  17. .CursorLocation = adUseClient
  18. .CursorType = adLockOptimistic
  19. .ActiveConnection = cn
  20. .Open
  21. End With
  22. End Sub
  23.  
  24. Public Function retrieve(ByVal recset As ADODB.Recordset, ByVal qry As String) As ADODB.Recordset
  25. With recset
  26. If recset.State = adStateOpen Then
  27. .Close
  28. End If
  29. .Source = qry
  30. .CursorType = adOpenKeyset
  31. .CursorLocation = adUseClient
  32. .LockType = adLockOptimistic
  33. .ActiveConnection = cn
  34. .Open
  35. End With
  36. Set retrieve = recset.DataSource
  37. End Function
  38.  
  39. Public Sub execute(ByVal qry As String)
  40. On Error GoTo Err
  41. With cn
  42. .BeginTrans
  43. .execute qry
  44. .CommitTrans
  45. End With
  46.  
  47. Err:
  48. cn.RollbackTrans
  49. MsgBox Err.description
  50. End Sub
  51.  
Oct 20 '07 #1
3 3493
debasisdas
8,127 Expert 4TB
Do not unload the second form just hide it and get back to the first form.
Oct 20 '07 #2
panget
4
I got rid of Unload Me. But the machine returned the error, "Syntax Error in INSERT INTO Statement"
Oct 22 '07 #3
panget
4
I've resolved the add problem. Thank you.

But now, I'd like to make an update button.

For form1, I will add this script:

--Form1--
Private Sub Command2_Click()
Form3.show vbmodal
Dim strQry2 As String
strQry2 = "update books set category = '" & n1 & "', description = '" & n2 & "', price = '" & n3 & "', contact_P = '" & n4 & "', store = '" & n5 & "', where category = '" & n1 & "'"
ab.execute strQry2
cd.Requery
Call Display
End Sub
---------

and for the new form

--Form3--
Public cd as new adodb.recordset

Private Sub Command3_Click()
With cd
If Not .EOF Then
.MoveNext
If .EOF Then
.MoveLast
End If
End If
Call Display
End With
End Sub
Private Sub Command2_Click()
With cd
If Not .BOF Then
.MovePrevious
If .BOF Then
.MoveFirst
End If
End If

Call Display
End With
End Sub

Public Sub Command4_Click()
Form1.n1 = catalog.Text
Form1.n2 = description.Text
Form1.n3 = price.Text
Form1.n4 = contactp.Text
Form1.n5 = store.Text
Form3.Hide
End Sub

----------
What should happen is if I click the Update button (Command2) on Form1, The contents of the flexgrid should appear on textboxes on Form3. I should be able to navigate them through the previous and next buttons (Command2 and 3 in Form3)
Nov 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: John | last post by:
I have a problem, it's not with any code I have because... there is no code. When I run a blank visual basic 6 form, it opens up just fine. When I add a text box, a caption, and a button... it...
2
by: janice_2k | last post by:
Dear Sir/Mdm, I am writing this on behalf of my company. We bought the Visual Studio .NET Enterprise version but currently we need to use Visual Basic 6 for a small development. We are not able to...
3
by: Ben | last post by:
Hi everyone, Now I am using Visual Basic.net to write a program but having some headaches. The situation is as follows: I am using ODBC connection to connect one database( non-SQL server) ,...
3
by: Silvia | last post by:
Hi, I have a form and I add buttons programmatically in de form_Load function. Anybody know how implements de function button_click if in the designer mode the button doesn't exists? I...
5
by: Microsoft | last post by:
Hi, I have Visual Basic .net 2003 (Standard Edition) & SQL Server 2000 Developer Edition. When trying to create a connection in the server explorer from the .net IDE I get a number of problems;...
0
by: Dr. Zharkov | last post by:
Hello. To see the graphics of technology DirectX 9.0 SDK Update - June 2005 in project Visual Basic, WindowsApplication1 from Visual Studio 2005 Beta 1, in file My Project, MyApplication.vb in a...
7
by: gerryLowry::Ability Business Computer Services {KC | last post by:
"Getting Back Your Visual Basic 6.0 Goodies" by Billy Hollis, 2003-5-14, states: "Getting a Forms Collection Visual Basic 6.0 developers are often fond of looping through the currently loaded...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
3
by: =?Utf-8?B?Rmxhc2hwcm8=?= | last post by:
i have googled this question but cannot find an answer. i'm running windows vista and i'm using Visual Basic Express 2008. i know the build event button SHOULD be in under the compile tag but i...
16
by: AirborneBob | last post by:
I just installed MS Office Enterprise on my system today and jumped right in to MS Access. Because I am familiar with Access I had very few problems navigating around. Here is the big question. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.