Results 1 to 40 of 67

Thread: Save to database.. Please help

Threaded View

  1. #31
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Save to database.. Please help

    In the addRecord sub you have to check which option button is selected and then save the value.
    Code:
    Private Sub addRecord()
    Dim cn As ADODB.Connection
    Dim sql As String
    Dim i As Integer
    
        For i = 0 To Option1.UBound
            If Option1(i).Value Then
                Exit For
            End If
        Next i
        
        sql = "Insert Into Table1(TrayPosition,Name,[Date],Spice,[Plant Type]) " & _
            "Values([TRAYPOSITION],'[NAME]','[DATE]','[SPICE]','[PLANTTYPE]')"
            
        sql = Replace(sql, "[TRAYPOSITION]", intBtnIndex)
        sql = Replace(sql, "[NAME]", Text1.Text)                  'Saves the Seed name to the database
        sql = Replace(sql, "[DATE]", Text3.Text)                  'The date the seed was planted
        sql = Replace(sql, "[SPICE]", Option1(i).Caption)     'The how hot the seed is
        sql = Replace(sql, "[PLANTTYPE]", Combo1.Text)            'The type of seed (example: vegtable)
        
        On Error GoTo HandleSaveErrors
        
        Set cn = GetConnection
        cn.Execute sql
        
        Set cn = Nothing
        
        blnAdded = True
    
    Exit Sub
    HandleSaveErrors:
        strMessage = "Record could not be saved. " & vbCrLf & Err.Description
                                
        MsgBox strMessage, vbExclamation, "Database Error"
    End Sub
    In reality, it would be much better to save the option button index instead of the caption. If you save the index you wouldn't need a Case statement to select the saved option button when you call FillFields in your Entry form. All you would need to do is
    Code:
    Option2(rs!Spice).Value = True
    If you run with this approach you need to change the Spice column in the table from Text datatype to Number and adjust the insert query so Spice is not wrapped in single quotes.
    Last edited by MarkT; Sep 26th, 2014 at 08:56 AM.

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