mysql addnew_AddNew 方法示例 (VB)

AddNew 方法示例 (VB)AddNew method example (VB)

09/18/2015

本文内容

适用于:Access 2013、Office 2013Applies to: Access 2013, Office 2013

以下示例使用 AddNew 方法创建一个具指定名称的新记录。This example uses the AddNew method to create a new record with the specified name.

'BeginAddNewVB

'To integrate this code

'replace the data source and initial catalog values

'in the connection string

Public Sub Main()

On Error GoTo ErrorHandler

'recordset and connection variables

Dim Cnxn As ADODB.Connection

Dim rstEmployees As ADODB.Recordset

Dim strCnxn As String

Dim strSQL As String

'record variables

Dim strID As String

Dim strFirstName As String

Dim strLastName As String

Dim blnRecordAdded As Boolean

' Open a connection

Set Cnxn = New ADODB.Connection

strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _

"Initial Catalog='Northwind';Integrated Security='SSPI';"

Cnxn.Open strCnxn

' Open Employees Table with a cursor that allows updates

Set rstEmployees = New ADODB.Recordset

strSQL = "Employees"

rstEmployees.Open strSQL, strCnxn, adOpenKeyset, adLockOptimistic, adCmdTable

' Get data from the user

strFirstName = Trim(InputBox("Enter first name:"))

strLastName = Trim(InputBox("Enter last name:"))

' Proceed only if the user actually entered something

' for both the first and last names

If strFirstName <> "" And strLastName <> "" Then

rstEmployees.AddNew

rstEmployees!firstname = strFirstName

rstEmployees!LastName = strLastName

rstEmployees.Update

blnRecordAdded = True

' Show the newly added data

MsgBox "New record: " & rstEmployees!EmployeeId & " " & _

rstEmployees!firstname & " " & rstEmployees!LastName

Else

MsgBox "Please enter a first name and last name."

End If

' Delete the new record because this is a demonstration

Cnxn.Execute "DELETE FROM Employees WHERE EmployeeID = '" & strID & "'"

' clean up

rstEmployees.Close

Cnxn.Close

Set rstEmployees = Nothing

Set Cnxn = Nothing

Exit Sub

ErrorHandler:

' clean up

If Not rstEmployees Is Nothing Then

If rstEmployees.State = adStateOpen Then rstEmployees.Close

End If

Set rstEmployees = Nothing

If Not Cnxn Is Nothing Then

If Cnxn.State = adStateOpen Then Cnxn.Close

End If

Set Cnxn = Nothing

If Err <> 0 Then

MsgBox Err.Source & "-->" & Err.Description, , "Error"

End If

End Sub

'EndAddNewVB

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值