Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Mar 2003
    Location
    Canton, Ohio, USA
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AddNew commands (VB 6.0/Access 2000)

    If I'm using the following code to enter data from a form into an Access table using text boxes, (rs!FirstName = txtFirstName.Text) how does the code change for entering data from a combo box or a check box? I can't seem to get it to work with them, only the text boxes. Also, If this code is in a Click Sub of an add button, what would I need to add to what I have to get all the fields to clear after the button is clicked?

    Dim rs As New ADODB.Recordset, sql As String
    Dim sConn As New ADODB.Connection

    sConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=CownloadsSampleSample.mdb;Persist Security Info=False"
    sConn.Open
    rs.CursorLocation = adUseClient
    rs.CursorType = adOpenKeyset
    sql = "SELECT FirstName, LastName FROM tblDecision"
    rs.Open sql, sConn, adOpenKeyset, adLockOptimistic, adCmdText
    rs.AddNew
    rs!FirstName = txtFirstName.Text
    rs!LastName = txtLastName.Text
    rs.Update
    rs.Close
    Set rs = Nothing

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: AddNew commands (VB 6.0/Access 2000)

    For a combo box, you should be able to use the Text property too, or ComboBoxName.List(ComboBoxName.ListIndex). For a check box, you can use the Value property (it is either True or False)

    Do you really want to clear all fields, or do you want to clear all controls (text boxes etc.)?

  3. #3
    2 Star Lounger
    Join Date
    Mar 2003
    Location
    Canton, Ohio, USA
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AddNew commands (VB 6.0/Access 2000)

    You are right Hans, it is the controls I want to clear so they are ready for new data if there is anymore to enter.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: AddNew commands (VB 6.0/Access 2000)

    You can either enumerate all editable controls and set their Text to "", ListIndex to -1 or Value to False, as appropriate, or you can loop through all controls (warning: air code ahead!)

    Dim ctl As Control
    For Each ctl In Me.Controls
    If TypeOf ctl Is TextBox Then
    ctl.Text = ""
    ElseIf TypeOf ctl Is CheckBox Then
    ctl.Value = False
    ElseIf TypeOf ctl Is ComboBox Then
    ctl.ListIndex = -1
    End If
    Next ctl
    Set ctl = Nothing

  5. #5
    2 Star Lounger
    Join Date
    Mar 2003
    Location
    Canton, Ohio, USA
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AddNew commands (VB 6.0/Access 2000)

    Thanks very much Hans

Posting Permissions

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