How to add data in access using vb6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vaniKanabathy
    New Member
    • Mar 2008
    • 52

    How to add data in access using vb6

    Hi, i need to extend my system functionality . I need to do add and edit function. Can anyone help me by guide how to do so ? I got error when i use this coding.
    [CODE=VB]
    Dim cn1 As ADODB.Connectio n
    Dim rs1 As ADODB.Recordset
    Dim sqlString As String

    ' Add new record -
    sqlString = "Insert INTO Alternator Engine,Volt,Amp )" _
    & " VALUES (" & txtAlternator.T ext & "," & txtVolt.Text & "," & txtAmp.Text & ")"

    Set rs1 = cn1.Execute(sql String)

    ' Clear the fields -
    txtAlternator.T ext = ""
    txtVolt.Text .Text = "" txtAmp.Text = ""[/CODE]
    Last edited by Dököll; Apr 4 '08, 04:13 AM. Reason: [CODE=VB] tag...
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    Your SQL ordering\syntax is wrong.

    The SQL syntax is as follows:
    [code=sql]
    INSERT INTO [TableName] (fieldname,fiel dname,fieldname ) VALUES ('" & Value1 & "', '" & Value2 & "', '" & Value3 & "')

    [/code]

    Your missing your table identifier. What table those fields are in.

    Also, I COULD be wrong, but I don't think you can have a field name in a table with a space in it (Alternator Engine is not a valid name for a column, if you go check your table design I'm betting thats not the name)

    So you need something like this instead:
    [code=vb]
    sqlString = "Insert INTO MyTable (AlternatorEngi ne,Volt,Amp)" _
    & " VALUES ('" & txtAlternator.T ext & "'," & txtVolt.Text & "," & txtAmp.Text & ")"
    [/code]

    Also note that I have placed single quote ' around the txtAlternator.T ext. This is because that is likely a string data type in your table design. Any string\text data type in a table will need the single quotes around it '" & x & "', if it is a number\integer\ long then you just need the " & x & "

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      #3
      Hey there!

      You've come to the right place, although I am not too skilled in ADODB. I can post something in DAO that works just great:-)

      Add reference to Microsoft DAO 3.06 Object Library

      [CODE=VB]
      Private Sub subt_Click() 'this funtion will load entry into database

      Dim my_database As Database
      Set my_database = OpenDatabase("C :\DataMining\Da ta_Central.mdb" )
      my_database.Exe cute "insert into Data_Central.Em ployee(UserID, EmployeeID, TodayDate, DOB,Title,Fname ,Mname,Lname,Su ffix, Address1,Addres s2,City,State,P ostalCode,Count ry,Gender,Marit alStatus,HomePh one,MobilePhone ,Fax,Email,Lice nseNum,Day,Mont h,Year,County) Values('" & Text1(0).Text & "','" & Text1(1).Text & "' , '" & Text1(2).Text & "' , '" & Text1(3).Text & "','" & Text1(4).Text & "' , '" & Text1(5).Text & "' ,'" & Text1(6).Text & "' ,'" & Text1(7).Text & "' ,'" & Text1(8).Text & "' ,'" & Text1(9).Text & "' ,'" & Text1(10).Text & "' ,'" & Text1(11).Text & "' ,'" & Text1(12).Text & "','" & Text1(13).Text & "' ,'" & Text1(14).Text & "' ,'" & Text1(15).Text & "' ,'" & Text1(16).Text & "' ,'" & Text1(17).Text & "' ,'" & Text1(18).Text & "' ,'" & Text1(19).Text & "' ,'" & Text1(20).Text & "' ,'" & Text1(21).Text & "' ,'" & Text1(22).Text & "','" & Text1(23).Text & "','" & Text1(24).Text & "','" & Text1(25).Text & "')"
      my_database.Clo se ' close the database

      End Sub
      [/CODE]

      You probably don't need all of the textboxes I have added...

      Have fun:-)
      Last edited by Dököll; Apr 4 '08, 04:19 AM. Reason: text, and comment...

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        If using adodb then try using this

        create a connection object
        open the connection
        [code=vb]
        con.BeginTrans
        con.execute "your insert statment goes here"
        'con.execute "your update statment goes here"
        con.CommitTrans
        [/code]

        Comment

        • jehanzeb20
          New Member
          • Jul 2008
          • 3

          #5
          I wanted to view the table of sqlite database in a datagrid or Flexgrid of VB 6.
          and allow datagrid for edit and update to the databae.
          Here is my code...
          But it showed all the table in a single row of flex grid :s




          Option Explicit

          '// SQL Lite dll declarations:

          Private Declare Sub sqlite3_open Lib "SQLite3VB. dll" (ByVal filename As String, ByRef handle As Long)
          Private Declare Sub sqlite3_close Lib "SQLite3VB. dll" (ByVal DB_Handle As Long)
          Private Declare Function sqlite3_last_in sert_rowid Lib "SQLite3VB. dll" (ByVal DB_Handle As Long) As Long
          Private Declare Function sqlite3_changes Lib "SQLite3VB. dll" (ByVal DB_Handle As Long) As Long
          Private Declare Function sqlite_get_tabl e Lib "SQLite3VB. dll" (ByVal DB_Handle As Long, ByVal SQLString As String, ByRef errstr As String) As Variant()
          Private Declare Function sqlite_libversi on Lib "SQLite3VB. dll" () As String ' Now returns a BSTR

          '// This function returns the number of rows from the last sql statement. Use this to ensure you have a valid array
          Private Declare Function number_of_rows_ from_last_call Lib "SQLite3VB. dll" () As Long




          Private Sub Command1_Click( )
          Dim dbfile As String
          Dim dbquery As String
          Dim db As Long
          Dim mvar As Variant
          Dim mv1 As Variant
          Dim errstr As String
          Dim row As Long
          Dim col As String
          Dim mstr As String
          Dim i As Long
          Dim L1 As ListItem
          Dim j As Long



          dbfile = App.Path & "\jas.db"
          dbquery = "select * from rec2"

          If dbfile = "" Or dbquery = "" Then
          MsgBox "Query not found:"
          Else
          sqlite3_open dbfile, db

          If db > 0 Then
          mvar = sqlite_get_tabl e(db, dbquery, errstr)
          row = number_of_rows_ from_last_call
          row = row + 1
          lbl1.Caption = row
          If row > 0 Then

          For Each mv1 In mvar
          mstr = mv1
          col = mstr

          If i < row Then
          DataGrid1.TextM atrix(i, 1) = col
          i = i + 1
          End If
          If i >= row Then

          DataGrid1.TextM atrix(i, 2) = col

          End If
          Next

          End If
          End If

          sqlite3_close db
          End If

          End Sub

          Comment

          Working...