bradyboyy88
Well-known Member
- Joined
- Feb 25, 2015
- Messages
- 562
So everytime I use ADO insert using the following code it causes a ' infront of my written value in the excel sheet. I have 2 functions, one to query and one to write to the excel file, and then test to run these sub routines. So in my test subroutine when i look at the excel file it shows 'TEST5 instead of just TEST5. I cant seem to figure this out. Thanks!
Code:
Option Explicit
Private Function SQLQueryDatabase(SQLQuery As String, StrDBPath As String, FieldNumber As Integer)
'Declare Variables
Dim oConn As Object
Dim oRs As Object
Dim sConn As String
'Define Connection String by inputting StrDBPath into a larger string (Works for Excel DB)
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & StrDBPath & "';" & _
"Extended Properties=""Excel 12.0;HDR=YES;ReadOnly=0;"";"
'Create Connection
Set oConn = CreateObject("ADODB.Connection")
'Create RecordSet
Set oRs = CreateObject("ADODB.Recordset")
'Connect to the database
oConn.Open sConn
'Open Record Set by executing SQL
oRs.Open SQLQuery, oConn
'Update the textbox with the only returned value
If oRs.EOF And oRs.BOF Then
SQLQueryDatabase = Null
Else
SQLQueryDatabase = oRs(FieldNumber).Value
End If
'Close Connection
oConn.Close
'Clear Memory
Set oConn = Nothing
Set oRs = Nothing
End Function
Private Sub SQLWriteDatabase(SQLQuery As String, StrDBPath As String)
'Declare Variables
Dim oConn As Object
Dim oRs As Object
Dim sConn As String
'Define Connection String by inputting StrDBPath into a larger string (Works for Excel DB)
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & StrDBPath & "';" & _
"Extended Properties=""Excel 12.0;HDR=YES;ReadOnly=0;"";"
'Create Connection
Set oConn = CreateObject("ADODB.Connection")
oConn.Mode = 3
'Create RecordSet
Set oRs = CreateObject("ADODB.Recordset")
'Connect to the database
oConn.Open sConn
'Open Record Set by executing SQL
oRs.Open SQLQuery, oConn
'Close Connection
oConn.Close
'Clear Memory
Set oConn = Nothing
Set oRs = Nothing
End Sub
Sub test()
Dim SQLString As String
Dim testvalue As Variant
SQLString = "Select * FROM [SHEET1$] WHERE NEW_NAME= 'TEST5'"
If IsNull(SQLQueryDatabase(SQLString, "C:\Users\User\Desktop\Test\book5.xlsx", 2)) Then
SQLString = "INSERT INTO [SHEET1$] (NEW_NAME, ROUND6) VALUES ('TEST5','22')"
Call SQLWriteDatabase(SQLString, "C:\Users\User\Desktop\Test\book5.xlsx")
Else
SQLString = "UPDATE [SHEET1$] SET ROUND5='25' WHERE NEW_NAME= 'TEST5'"
Call SQLWriteDatabase(SQLString, "C:\Users\User\Desktop\Test\book5.xlsx")
End If
End Sub