Results 1 to 20 of 20

Thread: [RESOLVED] VBA-Having trouble putting data in the database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    68

    Resolved [RESOLVED] VBA-Having trouble putting data in the database

    I made a form and i'm using the ADOB.connection to put the info into the database. But when I click the button an error comes up saying a problem occured with Microsoft communicating with OLE server or ActiveControl.

    Is there a way to just use SQL to put the info into the database from my form?
    This ADO/DAO stuff is not working for me.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: VBA-Having trouble putting data in the database

    here is an ADO tutorial courtesy of si_the_geek.

    It shows how to use SQL in ADO

    http://www.vbforums.com/showthread.php?t=337051


    HTH

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA-Having trouble putting data in the database

    The FAQ's that kfcSmitty linked to should be useful, especially Beacon's ADO Tutorial.

    If you show us the code you have already (and let us know where the error occurs) we may be able to offer more precise help.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    68

    Re: VBA-Having trouble putting data in the database

    Quote Originally Posted by si_the_geek
    The FAQ's that kfcSmitty linked to should be useful, especially Beacon's ADO Tutorial.

    If you show us the code you have already (and let us know where the error occurs) we may be able to offer more precise help.

    do you want me to post the entire code?
    it's like 500 lines

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: VBA-Having trouble putting data in the database

    I don't think he read your post.

    si -- he was asking how to connect to the database, no actual error was received or posted

    If you download this example, it should show you how to accomplish what you want.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    68

    Re: VBA-Having trouble putting data in the database

    Smitty isn't that tutorial for VB.NET tho?
    What syntax differences are there if any?

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA-Having trouble putting data in the database

    There are lots of differences with VB.Net, I'd recommend not looking at VB.Net code, but VB code instead (which is very similar to VBA).

    The ADO Tutorial is a good example, and may well be enough for you to spot the code you need - i not we can help.

    Quote Originally Posted by southphillyman
    do you want me to post the entire code?
    it's like 500 lines
    Nope, just the relevant part(s). eg: the code which has the error (the whole sub if it is fairly small), and the declarations for any variables/objects you use in that code.

    Quote Originally Posted by kfcSmitty
    si -- he was asking how to connect to the database, no actual error was received or posted
    From my interpretation of the first line, there is code, and it is raising an error.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    68

    Re: VBA-Having trouble putting data in the database

    this is the SQL code that has syntax problems

    DoCmd.RunSQL "INSERT INTO RepairOrders(CustomerName, CustomerAddress, " & _
    "CustomerCity, CustomerState, CustomerZIPCode, CarMakeModel, " & _
    "CarYear, ProblemDescription, Part1Name, Part1UnitPrice, " & _
    "Part1Quantity, Part1SubTotal, Part2Name, Part2UnitPrice, " & _
    "Part2Quantity, Part2SubTotal, Part3Name, Part3UnitPrice, " & _
    "Part3Quantity, Part3SubTotal, Part4Name, Part4UnitPrice, " & _
    "Part4Quantity, Part4SubTotal, Part5Name, Part5UnitPrice, " & _
    "Part5Quantity, Part5SubTotal, JobPerformed1, JobPrice1, " & _
    "JobPerformed2, JobPrice2, JobPerformed3, JobPrice3, " & _
    "JobPerformed4, JobPrice4, JobPerformed5, JobPrice5, " & _
    "TotalParts, TotalLabor, TaxRate, TaxAmount, RepairTotal, " & _
    "RepairDate, TimeReady, Recommendations)"

    "VALUES( [TxtCustomerName] , [CustomerAddress] ,& _
    [CustomerCity] , [CustomerState] , & _
    [CustomerZIPCode] , [txtCarMakeModel] , & _
    [CarYear] , [txtProblemDescription] , & _
    [Part1Name] , [Part1UnitPrice] , & _
    [Part1Quantity] , [Part1SubTotal] , & _
    [Part2Name], [Part2UnitPrice] , & _
    [Part2Quantity] , [Part2SubTotal] , & _
    [Part3Name] , [Part3UnitPrice] , & _
    [Part3Quantity] , [Part3SubTotal] , & _
    [Part4Name] , [Part4UnitPrice] , & _
    [Part4Quantity] , [Part4SubTotal] , & _
    [Part5Name] , [Part5UnitPrice] , & _
    [Part5Quantity] , [Part5SubTotal] , & _
    [JobPerformed1] , [JobPrice1] , & _
    [JobPerformed2] , [JobPrice2] , & _
    [JobPerformed3] , [JobPrice3] , & _
    [JobPerformed4] , [JobPrice4] , & _
    [JobPerformed5] , [JobPrice5] , & _
    [TotalParts] , [TotalLabor] , & _
    [TaxRate] , [TaxAmount] , & _
    [RepairTotal] , [RepairDate] , & _
    [TimeReady] , [Recommendations] );"

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA-Having trouble putting data in the database

    Ah yes... that is because you trying to put (what I assume are) control names into your SQL statement, rather than the values that are in the controls. (you also have a lack of quotes for the second half of that code).

    When using ADO you cannot refer to controls on Access forms, as ADO has no knowledge of what is going on in Access.

    See this thread from the FAQ for an explanation of how to do it.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    68

    Re: VBA-Having trouble putting data in the database

    Not sure I understand what you are talking about. So for the 2nd part of that code I should use the .text extension?
    And I'm not using ADO, I'm using SQL (I may be clueless in regards to what exactly ADO is tho)
    Also as far as the qoute's do I have to quote each line or each individual value?

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA-Having trouble putting data in the database

    oops, I missed the "DoCmd.RunSQL", ignore my previous post...

    The only issue is the Quotes around the second half of the code. The part from VALUES onwards needs to be in the same style as the previous code (with " at the start and end of the lines), and be on the line immediately after the earlier code (or build the SQL in a variable, and just pass that to the RunSQL method).

    eg:
    VB Code:
    1. "TotalParts, TotalLabor, TaxRate, TaxAmount, RepairTotal, " & _
    2. "RepairDate, TimeReady, Recommendations)" [b]& _[/b]
    3. "VALUES( [TxtCustomerName] , [CustomerAddress] , [b]"[/b] & _
    4. [b]"[/b][CustomerCity] , [CustomerState] , [b]"[/b] & _

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    68

    Re: VBA-Having trouble putting data in the database

    Still saying I have syntax errors.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    68

    Re: VBA-Having trouble putting data in the database

    It's saying I have too many line continuations.

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA-Having trouble putting data in the database

    Try
    Code:
    Dim sSQL As String
    sSQL = "INSERT INTO RepairOrders(CustomerName, CustomerAddress, " 
    sSQL = sSQL & "CustomerCity, CustomerState, CustomerZIPCode, CarMakeModel, "
    sSQL = sSQL & "CarYear, ProblemDescription, Part1Name, Part1UnitPrice, " 
    sSQL = sSQL & "Part1Quantity, Part1SubTotal, Part2Name, Part2UnitPrice, " 
    sSQL = sSQL & "Part2Quantity, Part2SubTotal, Part3Name, Part3UnitPrice, " 
    sSQL = sSQL & "Part3Quantity, Part3SubTotal, Part4Name, Part4UnitPrice, " 
    sSQL = sSQL & "Part4Quantity, Part4SubTotal, Part5Name, Part5UnitPrice, " 
    sSQL = sSQL & "Part5Quantity, Part5SubTotal, JobPerformed1, JobPrice1, " 
    sSQL = sSQL & "JobPerformed2, JobPrice2, JobPerformed3, JobPrice3, " 
    sSQL = sSQL & "JobPerformed4, JobPrice4, JobPerformed5, JobPrice5, " 
    sSQL = sSQL & "TotalParts, TotalLabor, TaxRate, TaxAmount, RepairTotal, " 
    sSQL = sSQL & "RepairDate, TimeReady, Recommendations) ,
    sSQL = sSQL & "VALUES( [TxtCustomerName] , [CustomerAddress] ,
    sSQL = sSQL & [CustomerCity] , [CustomerState] , 
    sSQL = sSQL & [CustomerZIPCode] , [txtCarMakeModel] , 
    sSQL = sSQL & [CarYear] , [txtProblemDescription] , 
    sSQL = sSQL & [Part1Name] , [Part1UnitPrice] , 
    sSQL = sSQL & [Part1Quantity] , [Part1SubTotal] , 
    sSQL = sSQL & [Part2Name], [Part2UnitPrice] , 
    sSQL = sSQL & [Part2Quantity] , [Part2SubTotal] , 
    sSQL = sSQL & [Part3Name] , [Part3UnitPrice] , 
    sSQL = sSQL & [Part3Quantity] , [Part3SubTotal] , 
    sSQL = sSQL & [Part4Name] , [Part4UnitPrice] , 
    sSQL = sSQL & [Part4Quantity] , [Part4SubTotal] , 
    sSQL = sSQL & [Part5Name] , [Part5UnitPrice] , 
    sSQL = sSQL & [Part5Quantity] , [Part5SubTotal] , 
    sSQL = sSQL & [JobPerformed1] , [JobPrice1] , 
    sSQL = sSQL & [JobPerformed2] , [JobPrice2] , 
    sSQL = sSQL & [JobPerformed3] , [JobPrice3] , 
    sSQL = sSQL & [JobPerformed4] , [JobPrice4] , 
    sSQL = sSQL & [JobPerformed5] , [JobPrice5] , 
    sSQL = sSQL & [TotalParts] , [TotalLabor] ,
    sSQL = sSQL & [TaxRate] , [TaxAmount] , 
    sSQL = sSQL & [RepairTotal] , [RepairDate] , 
    sSQL = sSQL &[TimeReady] , [Recommendations] "  
    DoCmd.RunSQL sSQL

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA-Having trouble putting data in the database

    ...with a few more quotes in
    eg:
    VB Code:
    1. sSQL = sSQL & "VALUES( [TxtCustomerName] , [CustomerAddress] , [u]"[/u]
    2. sSQL = sSQL & [u]"[/u][CustomerCity] , [CustomerState] , [u]"[/u]

  16. #16
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: VBA-Having trouble putting data in the database

    Quote Originally Posted by si_the_geek
    From my interpretation of the first line, there is code, and it is raising an error.

    Whoa, i totally must have been thinking of another post

  17. #17
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA-Having trouble putting data in the database

    Quote Originally Posted by si_the_geek
    ...with a few more quotes in
    eg:
    VB Code:
    1. sSQL = sSQL & "VALUES( [TxtCustomerName] , [CustomerAddress] , [u]"[/u]
    2. sSQL = sSQL & [u]"[/u][CustomerCity] , [CustomerState] , [u]"[/u]
    I did kinda miss some quotes there didn't I?

    Oh well, if he copy and pastes that into a code window, the IDE should put the ending quotes in.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    68

    Re: VBA-Having trouble putting data in the database

    Good Idea Hack. I will see if it works.
    Also what does the " []" do?

  19. #19
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA-Having trouble putting data in the database

    Quote Originally Posted by southphillyman
    Good Idea Hack. I will see if it works.
    I never use line continuations when writing SQL. It makes it so inflexible.
    Quote Originally Posted by southphillyman
    Also what does the " []" do?
    It is your query? Why do you have them there?

    Typically brackets are using to encapsulate field names that have a space in them (and if you create field names with a space in them, you should be whipped ), and to encapsulate field names that are reserved words like Date, Time, etc (another whipping for anyone using reserved words for field names )

    However, in reviewing your query, I don't see spaces or reserved words so I have no idea why they are being used.

  20. #20
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] VBA-Having trouble putting data in the database

    My assumption is that they are the names of controls on the form - and that Access will automagically replace the names with the values in the controls, and delimit them etc.

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