我看到了一个查询数据库的实例,仅一个窗体,内容如下列代码。
后来我自己照着他的控件布局新建立了一个工程,然后将下面代码全部照搬,心想这样clone一下肯定没问题,谁知在运行时总是报告:Database 未定义,请高手指点啊!!VERSION 5.00
Begin VB.Form Form1
Caption = "数据库查询"
ClientHeight = 2715
ClientLeft = 1620
ClientTop = 1545
ClientWidth = 3855
LinkTopic = "Form1"
ScaleHeight = 2715
ScaleWidth = 3855
Begin VB.ComboBox cboName
Height = 315
ItemData = "Form1.frx":0000
Left = 720
List = "Form1.frx":0010
Style = 2 'Dropdown List
TabIndex = 12
Top = 600
Width = 1215
End
Begin VB.CommandButton cmdCount
Caption = "查询"
Height = 495
Left = 600
TabIndex = 10
Top = 1680
Width = 1215
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "合计"
Height = 255
Index = 7
Left = 2040
TabIndex = 14
Top = 2040
Width = 495
End
Begin VB.Label lblCountTotal
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 13
Top = 2040
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "姓名"
Height = 255
Index = 6
Left = 120
TabIndex = 11
Top = 600
Width = 495
End
Begin VB.Label lblCountO
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 9
Top = 1680
Width = 495
End
Begin VB.Label lblCountF
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 8
Top = 1320
Width = 495
End
Begin VB.Label lblCountC
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 7
Top = 960
Width = 495
End
Begin VB.Label lblCountP
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 6
Top = 600
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "O"
Height = 255
Index = 5
Left = 2040
TabIndex = 5
Top = 1680
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "F"
Height = 255
Index = 4
Left = 2040
TabIndex = 4
Top = 1320
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "C"
Height = 255
Index = 3
Left = 2040
TabIndex = 3
Top = 960
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "P"
Height = 255
Index = 2
Left = 2040
TabIndex = 2
Top = 600
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "数量"
Height = 255
Index = 1
Left = 2880
TabIndex = 1
Top = 240
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "状态"
Height = 255
Index = 0
Left = 2040
TabIndex = 0
Top = 240
Width = 495
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option ExplicitPrivate Sub MakeData()
Dim dbString As String
Dim db As Database
Dim query As String
Dim i As Integer
Dim names(0 To 3) As String
Dim statuses(0 To 3) As String
Dim new_name As String
Dim new_status As String
Dim new_amount As Currency ' 打开数据库
dbString = App.Path & "\sales.mdb"
Set db = OpenDatabase(dbString)
' 获取数据
statuses(0) = "P"
statuses(1) = "C"
statuses(2) = "F"
statuses(3) = "O"
names(0) = "Remkus"
names(1) = "Stephens"
names(2) = "Johnson"
names(3) = "Smythe"
Randomize
For i = 1 To 50
new_name = names(Int(Rnd * 4))
new_status = statuses(Int(Rnd * 4))
new_amount = 50 + Rnd * 1000
query = "INSERT INTO sales Values(" & _
"'" & new_name & "', " & _
"'" & new_status & "', " & _
new_amount & ")"
db.Execute query
Next i
db.Close
End SubPrivate Sub cmdCount_Click()
Dim dbString As String
Dim db As Database
Dim rs As Recordset
Dim lstrSQL As String lstrSQL = ""
lstrSQL = lstrSQL & "Select Count(*) as TotalCount, "
lstrSQL = lstrSQL & "Count(iif([status] = 'P', 1, null)) as PCount, "
lstrSQL = lstrSQL & "Count(iif([status] = 'C', 1, null)) as CCount, "
lstrSQL = lstrSQL & "Count(iif([status] = 'F', 1, null)) as FCount, "
lstrSQL = lstrSQL & "Count(iif([status] = 'O', 1, null)) as OCount "
lstrSQL = lstrSQL & "From Sales "
lstrSQL = lstrSQL & "Where [Employee] = '" & _
cboName.Text & "'" dbString = App.Path & "\sales.mdb"
Set db = OpenDatabase(dbString)
' 查询
Set rs = db.OpenRecordset(lstrSQL, dbOpenSnapshot) ' 显示查询结果
lblCountTotal.Caption = Format$(rs(0))
lblCountP.Caption = Format$(rs(1))
lblCountC.Caption = Format$(rs(2))
lblCountF.Caption = Format$(rs(3))
lblCountO.Caption = Format$(rs(4)) rs.Close
db.Close
Set rs = Nothing
Set db = NothingEnd SubPrivate Sub Form_Load()
cboName.ListIndex = 0
End Sub
后来我自己照着他的控件布局新建立了一个工程,然后将下面代码全部照搬,心想这样clone一下肯定没问题,谁知在运行时总是报告:Database 未定义,请高手指点啊!!VERSION 5.00
Begin VB.Form Form1
Caption = "数据库查询"
ClientHeight = 2715
ClientLeft = 1620
ClientTop = 1545
ClientWidth = 3855
LinkTopic = "Form1"
ScaleHeight = 2715
ScaleWidth = 3855
Begin VB.ComboBox cboName
Height = 315
ItemData = "Form1.frx":0000
Left = 720
List = "Form1.frx":0010
Style = 2 'Dropdown List
TabIndex = 12
Top = 600
Width = 1215
End
Begin VB.CommandButton cmdCount
Caption = "查询"
Height = 495
Left = 600
TabIndex = 10
Top = 1680
Width = 1215
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "合计"
Height = 255
Index = 7
Left = 2040
TabIndex = 14
Top = 2040
Width = 495
End
Begin VB.Label lblCountTotal
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 13
Top = 2040
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "姓名"
Height = 255
Index = 6
Left = 120
TabIndex = 11
Top = 600
Width = 495
End
Begin VB.Label lblCountO
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 9
Top = 1680
Width = 495
End
Begin VB.Label lblCountF
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 8
Top = 1320
Width = 495
End
Begin VB.Label lblCountC
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 7
Top = 960
Width = 495
End
Begin VB.Label lblCountP
BorderStyle = 1 'Fixed Single
Height = 255
Left = 2880
TabIndex = 6
Top = 600
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "O"
Height = 255
Index = 5
Left = 2040
TabIndex = 5
Top = 1680
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "F"
Height = 255
Index = 4
Left = 2040
TabIndex = 4
Top = 1320
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "C"
Height = 255
Index = 3
Left = 2040
TabIndex = 3
Top = 960
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "P"
Height = 255
Index = 2
Left = 2040
TabIndex = 2
Top = 600
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "数量"
Height = 255
Index = 1
Left = 2880
TabIndex = 1
Top = 240
Width = 495
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "状态"
Height = 255
Index = 0
Left = 2040
TabIndex = 0
Top = 240
Width = 495
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option ExplicitPrivate Sub MakeData()
Dim dbString As String
Dim db As Database
Dim query As String
Dim i As Integer
Dim names(0 To 3) As String
Dim statuses(0 To 3) As String
Dim new_name As String
Dim new_status As String
Dim new_amount As Currency ' 打开数据库
dbString = App.Path & "\sales.mdb"
Set db = OpenDatabase(dbString)
' 获取数据
statuses(0) = "P"
statuses(1) = "C"
statuses(2) = "F"
statuses(3) = "O"
names(0) = "Remkus"
names(1) = "Stephens"
names(2) = "Johnson"
names(3) = "Smythe"
Randomize
For i = 1 To 50
new_name = names(Int(Rnd * 4))
new_status = statuses(Int(Rnd * 4))
new_amount = 50 + Rnd * 1000
query = "INSERT INTO sales Values(" & _
"'" & new_name & "', " & _
"'" & new_status & "', " & _
new_amount & ")"
db.Execute query
Next i
db.Close
End SubPrivate Sub cmdCount_Click()
Dim dbString As String
Dim db As Database
Dim rs As Recordset
Dim lstrSQL As String lstrSQL = ""
lstrSQL = lstrSQL & "Select Count(*) as TotalCount, "
lstrSQL = lstrSQL & "Count(iif([status] = 'P', 1, null)) as PCount, "
lstrSQL = lstrSQL & "Count(iif([status] = 'C', 1, null)) as CCount, "
lstrSQL = lstrSQL & "Count(iif([status] = 'F', 1, null)) as FCount, "
lstrSQL = lstrSQL & "Count(iif([status] = 'O', 1, null)) as OCount "
lstrSQL = lstrSQL & "From Sales "
lstrSQL = lstrSQL & "Where [Employee] = '" & _
cboName.Text & "'" dbString = App.Path & "\sales.mdb"
Set db = OpenDatabase(dbString)
' 查询
Set rs = db.OpenRecordset(lstrSQL, dbOpenSnapshot) ' 显示查询结果
lblCountTotal.Caption = Format$(rs(0))
lblCountP.Caption = Format$(rs(1))
lblCountC.Caption = Format$(rs(2))
lblCountF.Caption = Format$(rs(3))
lblCountO.Caption = Format$(rs(4)) rs.Close
db.Close
Set rs = Nothing
Set db = NothingEnd SubPrivate Sub Form_Load()
cboName.ListIndex = 0
End Sub
Project-->Preferences-->“Microsoft DAO 3.x Object Library”
中文版VB:
工程-->引用-->“Microsoft DAO 3.x Object Library”其中,3.x代表版本号,如3.51、3.6之类。