📄 frmmain.frm
字号:
Height = 285
Left = 840
TabIndex = 3
Top = 1680
Width = 2535
End
Begin VB.TextBox txtPhone
Height = 285
Left = 840
TabIndex = 2
Top = 1320
Width = 2535
End
Begin VB.TextBox txtName
Height = 285
Left = 840
Locked = -1 'True
TabIndex = 1
Top = 960
Width = 2535
End
Begin VB.Label lblEditStatus
BackStyle = 0 'Transparent
Caption = "Status:"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 120
TabIndex = 29
Top = 2520
Width = 1935
End
Begin VB.Label Label
Alignment = 1 'Right Justify
Caption = "Home:"
Height = 255
Index = 3
Left = 120
TabIndex = 9
Top = 1695
Width = 615
End
Begin VB.Label Label
Alignment = 1 'Right Justify
Caption = "Phone:"
Height = 255
Index = 2
Left = 120
TabIndex = 8
Top = 1335
Width = 615
End
Begin VB.Label Label
Alignment = 1 'Right Justify
Caption = "Name:"
Height = 255
Index = 1
Left = 120
TabIndex = 7
Top = 975
Width = 615
End
Begin VB.Label Label
Alignment = 1 'Right Justify
Caption = "Search For:"
Height = 255
Index = 0
Left = 120
TabIndex = 6
Top = 375
Width = 1095
End
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' ---------------------------------------------------
' Database Sample by Arnar Mar Sig
'
' When i first started working whit database i didin't know how to use Access Database, so
' i mode my own database by using plain Text, but doing that is not a good idia, and its slow too.
' So i searched on the Internet for samples how to use Access Database, and i searched and i
' serached and i found no useful sample, but i found lots of sample that had part of what i needed,
' and it took me long time to finish that wos left out. What i wanted to do wos not to use the DataList
' control that comes whit VB, that is slow and big, i wanted to use the DAO library.
' I know how long time and pissed i was figuring out how to use Access Databases, so i wrote this
' sample code how to use it, to help other programers that are starting to use Database.
'
' You can use this code, part of it ore all of it in any program you want whitout letting my know,
' but if you like it, then I would be realy happy if you E-Mail me and tell me what you thank of it.
' And if you need help whit it or other parts of Database programing, then I'll be happy to help you.
'
' My E-Mail Address is: the_dol@mi.is
' ---------------------------------------------------
Private Sub cmdAdd_Click()
rsData.AddNew ' To add now records we use AddNew function and then set its data.
rsData.Fields(0).Value = txtAddName.Text ' Set Row 0 data from txtAddName.Text
rsData.Fields(1).Value = txtAddPhone.Text ' Set Row 1 data from txtAddPhone.Text
rsData.Fields(2).Value = txtAddHome.Text ' Set Row 2 data from txtAddHome.Text
If chkAddWorking.Value = Checked Then ' Check if chkAddWorking is chacked or not
rsData.Fields(3).Value = True ' If checkd set Row 3 = true
Else
rsData.Fields(3).Value = False ' If checkd set Row 3 = false
End If
rsData.Update ' To add the record, we need to update it when we have set its data.
End Sub
Private Sub cmdDelete_Click()
rsData.MoveFirst ' Move to the first record in the Recordset ojbect.
' I find it easies to use Do loops when using database.
Do While Not rsData.EOF ' Loops until the Recordset is at end. (EOF = End Of File)
If LCase(rsData.Fields(0).Value) = LCase(txtToDelete.Text) Then 'First we need to find the record that we cant to delete.
' We have found the record now we delete it
rsData.Delete ' We delete records by using Delete.
lblStatus.Caption = "Status: Record found and deleted."
Exit Sub
End If
rsData.MoveNext ' Remember to move to the next record.
Loop
lblStatus.Caption = "Status: Not Found..." ' We didin't find any record
End Sub
Private Sub cmdEdit_Click()
rsData.MoveFirst ' Move to the first record in the Recordset ojbect.
Do While Not rsData.EOF ' Loops until the Recordset is at end. (EOF = End Of File)
If LCase(rsData.Fields(0).Value) = LCase(txtSearchFor.Text) Then ' First we need to find the record we are going to change
' Its the right record
rsData.Edit ' Set the Recordset object in Edit mode.
rsData.Fields(1).Value = txtPhone.Text ' Set Row 1 data from txtPhone.text
rsData.Fields(2).Value = txtHome.Text ' Set Row 2 data from txtHome.text
If chkWorking.Value = Checked = True Then ' Set Row 3 true or false
rsData.Fields(3).Value = True
Else
rsData.Fields(3).Value = False
End If
'When we are done changing the record we need to update it.
rsData.Update ' Update the Recordset object
lblEditStatus.Caption = "Status: Found and changed..."
Exit Sub
End If
rsData.MoveNext ' Remember to move to the next record.
Loop
lblEditStatus.Caption = "Status: Not Found..." ' We didin't find any record to update.
End Sub
Private Sub cmdSearch_Click()
rsData.MoveFirst ' Move to the first record in the Recordset ojbect.
' I find it easies to use Do loops when using database.
Do While Not rsData.EOF ' Loops until the Recordset is at end. (EOF = End Of File)
If LCase(rsData.Fields(0).Value) = LCase(txtSearchFor.Text) Then ' Check if txtSerarchFor.Text is the same as the first row in the database record. This counts from zero.
' If it is right then display the record data.
txtName.Text = rsData.Fields(0).Value ' Set txtName.text the data from Row 0
txtPhone.Text = rsData.Fields(1).Value ' Set txtPhone.text the data from Row 1
txtHome.Text = rsData.Fields(2).Value ' Set txtHome.text the data from Row 2
If rsData.Fields(3).Value = True Then ' Database can store Boolean values too
chkWorking.Value = Checked
Else
chkWorking.Value = Unchecked
End If
Exit Sub
End If
rsData.MoveNext ' Remember to move to the next record.
Loop
lblEditStatus.Caption = "Status: Not Found..." ' We didin't find any record
End Sub
Private Sub cmdSetToGrid_Click()
' Set the Grid Object.
flgGrid.Clear
flgGrid.Cols = 4
flgGrid.FixedCols = 0
flgGrid.FixedRows = 1
flgGrid.ColWidth(0) = flgGrid.Width / 4
flgGrid.ColWidth(1) = flgGrid.Width / 4
flgGrid.ColWidth(2) = flgGrid.Width / 4
flgGrid.ColWidth(3) = flgGrid.Width / 4
flgGrid.TextMatrix(0, 0) = "Name"
flgGrid.TextMatrix(0, 1) = "Phone"
flgGrid.TextMatrix(0, 2) = "Home"
flgGrid.TextMatrix(0, 3) = "Working"
flgGrid.Rows = 2
rsData.MoveFirst ' Move to the first record in the Recordset ojbect.
Do While Not rsData.EOF ' Loops until the Recordset is at end. (EOF = End Of File)
flgGrid.TextMatrix(flgGrid.Rows - 1, 0) = rsData.Fields(0).Value ' Set Grid Col 0 the data from Row 0
flgGrid.TextMatrix(flgGrid.Rows - 1, 1) = rsData.Fields(1).Value ' Set Grid Col 1 the data from Row 1
flgGrid.TextMatrix(flgGrid.Rows - 1, 2) = rsData.Fields(2).Value ' Set Grid Col 2 the data from Row 2
flgGrid.TextMatrix(flgGrid.Rows - 1, 3) = rsData.Fields(3).Value ' Set Grid Col 3 the data from Row 3
flgGrid.Rows = flgGrid.Rows + 1
rsData.MoveNext ' Remember to move to the next record.
Loop
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -