就教於各位大大:
個人曾受KeeKing指導之下,寫了form之VB
可以查詢卻無法增一筆資料,應如何處理?
Dim Book As Object
Dim Book1 As Object
Dim n
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Form_Initialize()
Dim fso, F As Object, Box, Tile$
Tile = "檔案路徑名稱"
On Error GoTo ER
Set fso = CreateObject("Scripting.FileSystemObject")
Set F = fso.GetFile("登錄.xls")
n = fso.GetParentFolderName(F.Path)
Set Book = CreateObject(n & "\登錄.xls")
Set Book1 = Book.sheets("學生資料")
For I = 1 To 6
Me.Controls("Label" & I).Caption = Book1.cells(1, I)
Next
Text1 = ""
Exit Sub
ER:
Set fs = CreateObject("Scripting.FileSystemObject")
Box = InputBox(Tile, Tile)
If Box = "" Then End
If fs.FileExists(Box) Then
Set Book = CreateObject(Box)
Else
GoTo ER
End If
Err.Clear
Resume Next
End Sub
Private Sub Text1_Change()
Dim Rng As Object, E, I%, C&
C = Book1.range("a1").End(4).Row
For Each E In Book1.range("a2:a" & C)
If E = Text1 Then Set Rng = E: Exit For
Next
If Not Rng Is Nothing Then
Book1.cells(C + 1, 1) = Text1
For I = 1 To 5
Me.Controls("Label" & I + 6).Caption = Rng.cells(1, I + 1)
Book1.cells(C + 1, I + 1) = Rng.cells(1, I + 1) '嘗試增加一筆資料,除非自行另外開.xls才會寫入
Next
Else
For I = 1 To 5
Me.Controls("Label" & I + 6).Caption = ""
Next
End If