私は現在、Excel VBA を使用して簡単な在庫システム アプリケーションを作成しています。入力を取得するためのユーザーフォームがあり、インポートデータをいくつかの Microsoft Access テーブルに保存したいと考えています。
ユーザーがEnterキーを押したときに、ユーザーフォームからデータを取得してアクセステーブルに入力しようとしています。このコードを実行すると、テーブル ID を使用して新しいレコードが作成されますが、インポートしようとしている 2 つのレコードは空白のままです。
Public Sub AddDatabaseEntry()
'Initialize all variables
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim stDB As String, stSQL As String, stProvider As String
Dim orderNum As String
Dim orderDate As String
orderNum = txtOrderNum
orderDate = txtDate
stDB = "Data Source= " & ThisWorkbook.Path & "\obsDatabase.accdb"
stProvider = "Microsoft.ACE.OLEDB.12.0"
'Opening connection to database
With cn
.ConnectionString = stDB
.Provider = stProvider
.Open
End With
'SQL Statement of what I want from the database
stSQL = "INSERT INTO Orders (OrderNumber, OrderDate) " & _
"Values ('" & orderNum & "', '" & orderDate & "')"
Set rs = cn.Execute(stSQL)
'Looping through the records I pulled and inserting the data into the comboBox
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
Private Sub btnAdd_Click()
AddProduct
AddDatabaseEntry
End Sub