Solved

Excel VBA - checking whether SQL append successful

Posted on 2011-09-09
3
359 Views
Last Modified: 2012-05-12
Hi

I am using the following Excel VBA code to append a record to a SQL database
I want to check whether the entry was successful by using a Select statement at
the point in the code where it says   '+++++ CHECK WHETHER ENTRY SUCCESSFUL
I am a little confused how to do this. There is an ID column which is automatically generated so I can't use this. Is there a way of selecting the last append to the database? Or should I do a selcting that includes all three fields entered?
Sub A()
    Call oAppend(Now, "tester", 2)
End Sub



Public Sub oAppend(ByVal oDate As Date, ByVal oText As String, ByVal oNumber As Single)
 
    Dim oSQL As String
    
    On Error GoTo EH
    
    Set con = New ADODB.Connection
    
    con.Open "Provider=SQLOLEDB;Data Source=196.2550.423.227,1444;Network Library=DBMSSOCN;Initial Catalog=test;User ID=murbroscave;Password=yabbadabbaidiot;"

    Set cmd = New ADODB.Command
    
    oSQL = "Insert Into Table1 ([oDate],[oText], [oNumber]) Select '" & oDate & "' as Expr1, '" & oText & "' as Expr2, " & oNumber & " as Expr3"
    
    With cmd
        .CommandText = oSQL
        .CommandType = adCmdText
        .ActiveConnection = con
        .Execute
    End With
  
    '+++++ CHECK WHETHER ENTRY SUCCESSFUL ++++++++++++++++++++++++++++++++++++++
    
    
    '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    Set cmd = Nothing
    Set con = Nothing
    
    Exit Sub
    
EH:
    
    MsgBox Err.Description
    
End Sub

Open in new window

0
Comment
Question by:murbro
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36510609
If you are only doing one record, why not open a recordset, add a record, populate it then Update the rs?
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36510622
PS You can also, I think do a SELECT @@IDENTITY query to get the last added ID.
0
 

Author Closing Comment

by:murbro
ID: 36510680
thanks for the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now