本人初次使用MS SQL7.0當作資料庫,嘗試撰寫客戶管理畫面。
'
' 在未使用「修改」功能按鈕之前,「首筆」、「上筆」、「下筆」、「末筆」功能按鈕可以正常運作。
' 當使用用「修改」功能按鈕之後,並按「寫入」功能按鈕之後,則會當掉,出現的MSGBOX 為
' 鍵欄位資訊不足或不正確;大多列被更新所影響。
'
' 之前,VB + ACCESS 2000正常運作,為什麼 VB + SQL 7.0則無法正常運作。
'
' 請各位高手指點迷津,在此先行言謝!
'
' 程式碼節略如下:
'=========================================================================================
' 目 的: 功能按鈕
'=========================================================================================
Private Sub Form_Load()
...
'------------------------------------------------
' D0 資料控制項
'------------------------------------------------
With adoPrimaryRS
.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI; " _
& "Persist Security Info=False;Initial Catalog=Client; " _
& "Data Source=QA_0001"
.CommandType = adCmdTable
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.RecordSource = "tblclient"
.Refresh
End With
...
End Sub
'=========================================================================================
' 目 的: 功能按鈕
'=========================================================================================
Private Sub tbrTool_ButtonClick(ByVal Button As MSComctlLib.Button)
Dim at As Variant
On Error Resume Next
Select Case Trim(Button.Description)
Case "新增"
at = adoPrimaryRS.Recordset.AbsolutePosition
adoPrimaryRS.Recordset.AddNew
mstrEditMode = "新增"
Case "修改"
at = adoPrimaryRS.Recordset.AbsolutePosition
mstrEditMode = "修改"
Case "刪除"
Delete
Case "取消"
adoPrimaryRS.Recordset.CancelUpdate
adoPrimaryRS.Move 0
mstrEditMode = ""
Case "寫入"
Select Case mstrEditMode
Case "新增"
lblDateCreated.Caption = Now()
Case "修改"
lblDateModified.Caption = Now()
End Select
Save
mstrEditMode = ""
...
Case "首筆"
adoPrimaryRS.Recordset.MoveFirst
Case "上筆"
adoPrimaryRS.Recordset.MovePrevious
If adoPrimaryRS.Recordset.BOF Then
adoPrimaryRS.Recordset.MoveFirst
End If
Case "下筆"
adoPrimaryRS.Recordset.MoveNext
If adoPrimaryRS.Recordset.EOF Then
adoPrimaryRS.Recordset.MoveLast
End If
Case "末筆"
adoPrimaryRS.Recordset.MoveLast
...
End Select
End Sub
'=========================================================================================
' 目 的: 儲存
'=========================================================================================
Private Sub Save()
If adoPrimaryRS.Recordset.EditMode = adEditAdd Then
On Error GoTo SaveFailure:
adoPrimaryRS.Recordset.Update
End If
SaveDone:
Exit Sub
SaveFailure:
MsgBox Err.Number & vbCrLf & Err.Description
Resume SaveDone
End Sub
SPENCER YANG
------------
2000/01/26