Results 1 to 6 of 6

Thread: database newbie question?

  1. #1

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    how do i add records and delete them via code?

  2. #2
    Member
    Join Date
    Aug 2000
    Posts
    60
    Try this:

    Using Database and Recordset objects for DAO or
    Connection and Recordset abjects ADO.

    Move to the record that you want to delete by using either
    one of the move methods (movefirst, moveprevious, movenext, movelast) or the findfirst/find method.

    The just call the delete method, as in: recordset.Delete
    Barend
    JHB-SA

    Nothing is impossible, except skiing through a revolving door.

  3. #3

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    and do you have any code that would help?

  4. #4
    Addicted Member
    Join Date
    Aug 1999
    Location
    Hamilton, New Zealand
    Posts
    137
    Code:
    Dim cnnDB as new ADODB.connection
    Dim rstTemp as new ADODB.recordset
    Dim strCnn as string
    
    'Connect to a local database using Jet
    strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp.mdb;"
    cnnDB.Mode = adModeShareDenyNone
    cnnDB.Open StrCnn
    
    'Open a recordset
    rstTemp.open "SELECT * FROM temp", cnnDB
    
    'Add a record
    With rstTemp
      .addNew
      !Field1 = Value1
      !Field2 = Value 2
      'and so on
    End With
    
    'To delete the current record
    rstTemp.delete
    
    'After either of these methods you should call
    rstTemp.update
    
    'Clean up
    rstTemp.close
    cnnDB.close
    Set rstTemp = Nothing
    Set cnnDB = Nothing
    This is just a basic guide.

    www.microsoft.com/data

    has more info under the ADO section

    Regards

    Matt Brown
    Hamilton, NZ
    VB6 C++ in Visual Studio 6sp4
    VB.net Beta 1

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Option Explicit
    
    Public cDBName As String
    Public cTblName As String
    
     
    Public Sub OpenDB()
    
       Dim db As Database, rs As Recordset
       Dim AppPath$
                
            If Right(App.Path, 1) <> "\" Then _
                AppPath = App.Path & "\" _
            Else AppPath = App.Path
            
                cDBName = AppPath & "YourDatabaseName.mdb"
            
                cTblName = "YourTable"
          Set db = Workspaces(0).OpenDatabase(cDBName)
          Set rs = db.OpenRecordset(cTblName)
                Data1.DatabaseName = cDBName
                Data1.RecordSource = cTblName
             
                Data1.Refresh
    
         End Sub
    
    Private Sub Command1_Click()
        Data1.Recordset.AddNew
        Data1.Recordset!YourField = Text1.Text
        Data1.Recordset.Update
        Data1.Refresh
        Command1.Enabled = False
        
        
    End Sub
    
    Private Sub Form_Activate()
    '
          Call OpenDB
    '
          
          Form1.Data1.Recordset.movefirst
        
    End Sub
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''
    ' sub to process delete function in a database
    ' once you are at the position on the record 
    'call this routine to do delete a record from your database
    
    Public Sub DeleteRec()
    '
    ' give user option of deleting or escaping from delete function
    ' datName is the name of my data control
    ' RecordCount is a global variable containing the recordcount
    
        Dim intRtn
    '
        intRtn = MsgBox("Do you really wish to delete this record?", _
                   vbYesNo, "Delete Function")
    ' if user wishes to delete then delete and move to previous record 
    '
           If intRtn = 6 Then
                datName.Recordset.Delete
            If datName.Recordset.AbsolutePosition + 1 = datName.Recordset.RecordCount Then
                datName.Recordset.MovePrevious
                Else
                datName.Recordset.MoveNext
            End If
            
    	Else
    	   
                Exit Sub  'if user wishes to escape the function...quit sub
    '
            End If
    '
    'call from event on form
    	Call DeleteRec
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    thank you very much for your help

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