Results 1 to 5 of 5

Thread: INSERT Statements. How to do it!

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2003
    Posts
    46

    INSERT Statements. How to do it!

    Hi,
    Im wanting to insert a date value (quote_date) into a a table called "quotes". I have formatted into my database for my VB application. Can someone give me some pointers. Here is my code so far

    Public Sub InsertQuote(strQuoteDate As Date)
    Dim strSql As String

    strSql = "insert into quotes (quote_date) values(" & strQuoteDate & ")"

    oConn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
    "Dbq=c:\irs\irs1.mdb;" & _
    "Uid=admin;" & _
    "Pwd="


    End Sub

    Basically, How do I execute it and format it for my access database to accept?

    Andy

  2. #2
    Lively Member
    Join Date
    Nov 2002
    Location
    Hong Kong
    Posts
    83

    Re: INSERT Statements. How to do it!

    Originally posted by andycharger
    Hi,
    Im wanting to insert a date value (quote_date) into a a table called "quotes". I have formatted into my database for my VB application. Can someone give me some pointers. Here is my code so far

    Public Sub InsertQuote(strQuoteDate As Date)
    Dim strSql As String

    strSql = "insert into quotes (quote_date) values(" & strQuoteDate & ")"

    oConn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
    "Dbq=c:\irs\irs1.mdb;" & _
    "Uid=admin;" & _
    "Pwd="


    End Sub

    Basically, How do I execute it and format it for my access database to accept?

    Andy
    strQuoteDate FORMAT: "#DATE#"

    Execute: oConn.Execute strSql

  3. #3
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    VB Code:
    1. Public Sub InsertQuote(strQuoteDate As Date)
    2.     Dim oConn As ADODB.Connection
    3.  
    4.     Set oConn = New ADODB.Connection
    5.  
    6.     With oConn
    7.         .ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
    8.                             "Dbq=c:\irs\irs1.mdb;" & _
    9.                             "Uid=admin;" & _
    10.                             "Pwd="
    11.    
    12.         .Execute "INSERT INTO quotes (quote_date) VALUES (#" & strQuoteDate & "#)"
    13.     End With
    14.    
    15.     Set oConn = Nothing
    16. End Sub
    ~seaweed

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2003
    Posts
    46

    Thank You

    That is great except, the connectionstring command did not work. I changed it to .open instead and it does. However, if I want to pass more than one value from the form, I thought I would do it like this:
    insertQuote( strQuoteDate as Date, strQuoteNumber as String)
    However, I get anerror saying:
    Compile Error

    Expected: List seperator or )

    How do I get around this?

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    strSQL = "INSERT INTO TableNameHere "
    strSQL = strSQL & "(TableColumn1Name, TableColumn2Name) " 
    strSQL = strSQL & "VALUES ( '" & FormControlColumn1Val & "', '" & FormControlColumn2Val & "')"
    
    oConn.execute strSQL
    
    If oConn.errors.count > 0 Then
        Msgbox "Error occurred!!!"
    End If


    Notice that I've used ' characters there, so if the statement will come out like:
    Code:
    "INSERT INTO table1 (column1) VALUES ('Textbox1TextVal')"
    You only need to enclose string values in single quotes, if you're dealing with numbers (i.e. like passing a 0 or 1 value back from a checkbox control value, you don't enclose these values in the single quotation marks.
    Last edited by alex_read; Mar 10th, 2003 at 06:40 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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