Access でコーディングを開始し、テーブルに行を追加する関数を作成しようとしましたが、これは機能しません。
「FirstName」と「LastName」の 2 つの列と、次のコードを起動するボタンを含む単純なテーブル (Table1) を作成しました。
Private Sub Command0_Click()
AppendRow "Table1", "John", "Doe"
End Sub
AppendRow の場所:
Function AppendRow(toTableName As String, firstName As String, lastName As String) As Boolean
' Returns True on success, false otherwise
' USAGE: AppendRow "toTableName", "firstName", "lastName"
On Error GoTo errhandler
Dim strSql As String
'Create the SQL string
strSql = "INSERT INTO " & toTableName & " (FirstName, LastName) " & _
"VALUES ('" & firstName & "', '" & lastName & "');"
'Print the SQL so we can paste into the query build if there are errors
Debug.Print strSql
MsgBox strSql
'Run the SQL Query
CurrentDb.Execute strSql
'If no errors return true
AppendRow = True
ExitHere:
Exit Function
errhandler:
'There is an error return false
AppendRow = False
With Err
MsgBox "Error " & .Number & vbCrLf & .Description, vbOKOnly Or vbCritical, "AppendTable"
End With
Resume ExitHere
End Function
SQL文字列は次のようになります
INSERT INTO Table1 (FirstName, LastName) VALUES ('John', 'Doe')
編集:欠落している引用符を追加しました。