VBA & ADO: INSERT INTO Blank worksheet, Field Types

dageci

New Member
Joined
Mar 10, 2016
Messages
16
Hello,
do you have any tips on how do you insert a record into a BLANK worksheet using SQL INSERT INTO and that the columns are recognized properly?
For example, if we have a worksheet with 1 Number field, 1 date field, and 1 string field, and if we use the INSERT INTO when the worksheet in empty it will insert all the fields as a string (it will add the apostrophe in front)


Code example:

Code:
Private Sub InsertData()
'add a reference to Microsoft ActiveX Data Objects 2.8 Library
Dim cn As New ADODB.Connection
Dim sSQL As String
Dim sConStr As String


    sSQL = "INSERT INTO [Sheet1$] (ID, MyDate, StringColumn) VALUES (2,#2019/12/14#, 'Test string')"
    
    sConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.FullName & ";Extended Properties=""Excel 8.0;HDR=Yes;"";"


    cn.Open sConStr
    
    cn.Execute sSQL


Extit_sub:
    If cn.State = 1 Then cn.Close
    Set cn = Nothing
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top