Solved

Excel VBA - checking whether SQL append successful

Posted on 2011-09-09
3
363 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

808 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