Results 1 to 5 of 5

Thread: Writing to a database

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    29

    Question

    I need to be able to take data from a text box and write it back to Access using Visual Basic 6. I have been using ADO throughout the project and have been able to update existing records in the database but I`m unsure of how to create new records using the data supplied in the text boxes, etc. Any help would be appreciated..

  2. #2
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696

    Talking

    Hi spooked this is nice and easy to do.

    Use the following code and it should sort out your problem

    Code:
    Public sub AddRecords()
    Dim myRecordset As ADODB.recordset
    Dim myConnection As ADODB.connection
    Dim strSql As String
    Dim strCon As String
    
    strCon = 'put in your connection string here
    
    Set myConnection = New ADODB.connection
    Set myRecordset = New ADODB.recordset
    
    myConnection.Open strCon
    
    strSql = "SELECT * FROM tblTest" ' Replace this with your table
    
    myRecordset.Open strSql,myConnection ,adOpenDynamic
    
    myRecordset.addnew
    'replace theese with your field names and you textboxes
    myRecordset("SOMEFIELD") = txtsometextbox
    myRecordset("SOMEFIELD1") = txtsometextbox1
    myRecordset("SOMEFIELD2") = txtsometextbox2
    myRecordset("SOMEFIELD3") = txtsometextbox3
    myRecordset.Update
    
    myConnection.close
    
    Set myConnection = Nothing
    Set myRecordset = Nothing
    Hope this helps

    Ian
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  3. #3
    Addicted Member JasonGS's Avatar
    Join Date
    May 2000
    Location
    California
    Posts
    155

    a little easier...

    a quicker method with a little less code and a way quicker recordset transaction...
    Code:
    dbFields = Array("Text1", "Text2", "Text3")
    dbValues = Array(Text1.Text, Text2.Text, Text3.Text)
    
    adoRset.Open "table", adoConn, adOpenStatic, adLockOptimistic
    adoRset.AddNew dbFields, dbValues
    adoRset.Close

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Exclamation INSERT INTO Statement just enough

    May be INSERT INTO statement is fastest to do this,

    Code:
    Dim xSQL$
    Dim lngAffectedRcd&
    Dim ADOConn As New ADODB.Connection
    Set ADOConn = Bla...Bla...Bla...
    
    xSQL = "INSERT INTO TblMyTable VALUES ('" & Text1.Text & "', '" & Text2.Text & "');" 
    ADOConn.Execute xSQL, lngAffectedRcd, adExecuteNoRecords
    Assume TblMyTable only have 2 Text data fields.

    [Edited by Chris on 06-02-2000 at 09:46 PM]

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If the TextBoxes are mapped to the datafields already why don't you just use ADO.addnew before they type the info in the boxes (it will even clear the boxes for a new entry) and then if they change records or add again it will automatically save the info or you can have a save button and just use ADO.update in the click event

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width