inserting data to access table from excel userform

Sagar0650

Board Regular
Joined
Nov 25, 2019
Messages
55
Office Version
  1. 2019
Platform
  1. Windows
i have a table created in access database i.e mytable
this table has fields as A, B, C, D ,E , F, G , H , I , J , K, L, M , N, O, P, Q, R, S
this field names are just to practice
now i have written a code on userform button so that when i hit the button data gets added to table

below is the code

VBA Code:
Private Sub CommandButton3_Click()

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset

Dim path As String, btext As String, dtext As String
Dim etext As String, ptext As String, stext As String

btext = TextBox6.Text
dtext = TextBox7.Text
etext = TextBox3.Text
ptext = TextBox4.Text
stext = TextBox5.Text

path = "C:\Users\Documents\demo.accdb"

Set conn = New ADODB.Connection

conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path

Set rs = New ADODB.Recordset

Sql = "INSERT INTO mytable([B], [D], [E], [P], [S],)values(" & btext & " , " & dtext & " , " & etext & " , " & ptext & " , " & stext & ")"
conn.Execute Sql

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Sagar0650

Board Regular
Joined
Nov 25, 2019
Messages
55
Office Version
  1. 2019
Platform
  1. Windows
here i want to insert data into only specific fields that is the reason why i have added only few column names in the sql query above
dont know if this is possible or no?

i am getting an error msg that "syntax error in INSERT INTO statement"

can someone please help me with this?
 

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
2,259
Office Version
  1. 365
Platform
  1. Windows
& btext & " , " & dtext & " , " & etext & " , " & ptext & " , " & stext
If they are text, they need to be delimited:
('" & btext & "' , '" & dtext & "' , '" & etext & "' ,' " & ptext & "' , '" & stext & "')"
 

Sagar0650

Board Regular
Joined
Nov 25, 2019
Messages
55
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

it worked
thank you sir
 

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
2,259
Office Version
  1. 365
Platform
  1. Windows
You're welcome. If you did Debug.Print as suggested but for both versions you might see the difference more clearly. Just a thought...
 

Forum statistics

Threads
1,143,676
Messages
5,720,242
Members
422,272
Latest member
ginkgoVil

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
Top