我用的不是数据绑定,代码如下,求哪位大虾能帮我看看 主模块相关代码: Public Function ConnectString() _ As String 'returns a DB ConnectString ConnectString = "FileDSN=大华塑业备件数据库.dsn;UID=sa;PWD=sa" End Function Public Function ExecuteSQL(ByVal SQL _ As String, MsgString As String) _ As ADODB.Recordset 'executes SQL and returns Recordset Dim cnn As ADODB.Connection Dim rst As ADODB.Recordset Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL) Set cnn = New ADODB.Connection cnn.Open ConnectString If InStr("INSERT,DELETE,UPDATE,EXECUTE", _ UCase$(sTokens(0))) Then cnn.Execute (SQL) MsgString = sTokens(0) & _ " query successful" Else Set rst = New ADODB.Recordset rst.Open Trim$(SQL), cnn, _ adOpenKeyset, _ adLockOptimistic 'rst.MoveLast 'get RecordCount Set ExecuteSQL = rst MsgString = "查询到" & rst.RecordCount & _ " 条记录 " End If ExecuteSQL_Exit: Set rst = Nothing Set cnn = Nothing Exit Function
ExecuteSQL_Error: MsgString = "查询错误: " & _ Err.Description Resume ExecuteSQL_Exit End Function 窗体相关代码: Private Sub From_Load() ShowTile ShowData 'cnn.Open ConnectString ' DisplayGrid End Sub Private Sub ShowTile() Dim i As Integer With MSFlexGrid1 '设置列数目 .Cols = 8 .TextMatrix(0, 1) = "物资编号" .TextMatrix(0, 2) = "名称" .TextMatrix(0, 3) = "型号" .TextMatrix(0, 4) = "规格" .TextMatrix(0, 5) = "计量单位" .TextMatrix(0, 6) = "库存量" .TextMatrix(0, 7) = "存放位置" .TextMatrix(0, 8) = "备注" ' 固定表头 .FixedRows = 1 '设置各列的对齐方式 For i = 0 To 8 .ColAlignment(i) = 0 Next i '表头项居中 .FillStyle = flexFillRepeat .Col = 0 .Row = 0 .RowSel = 1 .ColSel = .Cols - 1 .CellAlignment = 4 ''设置单元大小 .ColWidth(0) = 1000 .ColWidth(1) = 1000 .ColWidth(2) = 1000 .ColWidth(3) = 1000 .ColWidth(4) = 1000 .ColWidth(5) = 1000 .ColWidth(6) = 1000 .ColWidth(7) = 1000 .ColWidth(8) = 1000 .Row = 1 End With End Sub Private Sub ShowData() Dim j As Integer Dim i As Integer Dim MsgText As String txtSQL = "SELECT * FROM 库存信息表" Set mrc = ExecuteSQL(txtSQL, MsgText) With MSFlexGrid1 .Rows = 1
Do While Not mrc.EOF .Rows = .Rows + 1 For i = 1 To mrc.Fields.Count '判断数据项是否为空 If Not IsNull(Trim(mrc.Fields(i - 1))) Then '根据数据项类型进行 Select Case mrc.Fields(i - 1).Type '如果是日期型数据,按照"年-月-日"格式化 Case adDBData .TextMatrix(.Rows - 1, i) = Format(mrc.Fields(i - 1) & "", "yyyy-mm-dd") Case Else .TextMatrix(.Rows - 1, i) = mrc.Fields(i - 1) & "" End Select End If Next i mrc.MoveNext Loop
End With mrc.Close End Sub 操作相关代码: Private Sub cmdSave_Click() Dim intCount As Integer Dim sMeg As String Dim MsgText As String Dim mrc As ADODB.Recordset '判断输入内容是否为空
'判断是否输入数字
'添加判断是否有相同的ID记录 If gint库存信息设置修改mode = 1 Then txtSQL = "select * FROM [dbo].[库存信息表] where 物资编号='" & Trim(txtFields(0)) & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) If mrc.EOF = False Then MsgBox "已经存在此物资编号的记录!", vbOKOnly + vbExclamation, "警告" txtFields(0).SetFocus Exit Sub End If mrc.Close End If '先删除已有记录 txtSQL = "delete from 库存信息表 where 物资编号='" & Trim(txtFields(0)) & " '" Set mrc = ExecuteSQL(txtSQL, MsgText) '再加入新记录 txtSQL = "execute 库存信息表_setup'" '调用事先建立的SQL存储过程 For intCount = 0 To 7 txtSQL = txtSQL & Trim(txtFields(intCount)) & "','" txtSQL = txtSQL & Trim(txtFields(intCount)) & "," Next intCount '执行SQL语句 Set mrc = ExecuteSQL(txtSQL, MsgText) '更新窗体 If gint库存信息设置修改mode = 1 Then MsgBox "添加记录成功!", vbOKOnly + vbExclamation, "添加记录" For intCount = 0 To 7 txtFields(intCount) = "" Next intCount Unload Me frm库存信息设置修改.txtSQL = "select* FROM [dbo].[库存信息表]" frm库存信息设置修改.Show ElseIf gint库存信息设置修改mode = 2 Then Unload Me frm库存信息设置修改.txtSQL = "select* FROM [dbo].[库存信息表]" frm库存信息设置修改.Show End If End Sub
主模块相关代码:
Public Function ConnectString() _
As String
'returns a DB ConnectString
ConnectString = "FileDSN=大华塑业备件数据库.dsn;UID=sa;PWD=sa"
End Function
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE,EXECUTE", _
UCase$(sTokens(0))) Then
cnn.Execute (SQL)
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function
窗体相关代码:
Private Sub From_Load()
ShowTile
ShowData
'cnn.Open ConnectString
' DisplayGrid
End Sub
Private Sub ShowTile()
Dim i As Integer
With MSFlexGrid1
'设置列数目
.Cols = 8
.TextMatrix(0, 1) = "物资编号"
.TextMatrix(0, 2) = "名称"
.TextMatrix(0, 3) = "型号"
.TextMatrix(0, 4) = "规格"
.TextMatrix(0, 5) = "计量单位"
.TextMatrix(0, 6) = "库存量"
.TextMatrix(0, 7) = "存放位置"
.TextMatrix(0, 8) = "备注"
' 固定表头
.FixedRows = 1
'设置各列的对齐方式
For i = 0 To 8
.ColAlignment(i) = 0
Next i
'表头项居中
.FillStyle = flexFillRepeat
.Col = 0
.Row = 0
.RowSel = 1
.ColSel = .Cols - 1
.CellAlignment = 4
''设置单元大小
.ColWidth(0) = 1000
.ColWidth(1) = 1000
.ColWidth(2) = 1000
.ColWidth(3) = 1000
.ColWidth(4) = 1000
.ColWidth(5) = 1000
.ColWidth(6) = 1000
.ColWidth(7) = 1000
.ColWidth(8) = 1000
.Row = 1
End With
End Sub
Private Sub ShowData()
Dim j As Integer
Dim i As Integer
Dim MsgText As String
txtSQL = "SELECT * FROM 库存信息表"
Set mrc = ExecuteSQL(txtSQL, MsgText)
With MSFlexGrid1
.Rows = 1
Do While Not mrc.EOF
.Rows = .Rows + 1
For i = 1 To mrc.Fields.Count
'判断数据项是否为空
If Not IsNull(Trim(mrc.Fields(i - 1))) Then
'根据数据项类型进行
Select Case mrc.Fields(i - 1).Type
'如果是日期型数据,按照"年-月-日"格式化
Case adDBData
.TextMatrix(.Rows - 1, i) = Format(mrc.Fields(i - 1) & "", "yyyy-mm-dd")
Case Else
.TextMatrix(.Rows - 1, i) = mrc.Fields(i - 1) & ""
End Select
End If
Next i
mrc.MoveNext
Loop
End With
mrc.Close
End Sub
操作相关代码:
Private Sub cmdSave_Click()
Dim intCount As Integer
Dim sMeg As String
Dim MsgText As String
Dim mrc As ADODB.Recordset
'判断输入内容是否为空
'判断是否输入数字
'添加判断是否有相同的ID记录
If gint库存信息设置修改mode = 1 Then
txtSQL = "select * FROM [dbo].[库存信息表] where 物资编号='" & Trim(txtFields(0)) & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = False Then
MsgBox "已经存在此物资编号的记录!", vbOKOnly + vbExclamation, "警告"
txtFields(0).SetFocus
Exit Sub
End If
mrc.Close
End If
'先删除已有记录
txtSQL = "delete from 库存信息表 where 物资编号='" & Trim(txtFields(0)) & " '"
Set mrc = ExecuteSQL(txtSQL, MsgText)
'再加入新记录
txtSQL = "execute 库存信息表_setup'" '调用事先建立的SQL存储过程
For intCount = 0 To 7
txtSQL = txtSQL & Trim(txtFields(intCount)) & "','"
txtSQL = txtSQL & Trim(txtFields(intCount)) & ","
Next intCount
'执行SQL语句
Set mrc = ExecuteSQL(txtSQL, MsgText)
'更新窗体
If gint库存信息设置修改mode = 1 Then
MsgBox "添加记录成功!", vbOKOnly + vbExclamation, "添加记录"
For intCount = 0 To 7
txtFields(intCount) = ""
Next intCount
Unload Me
frm库存信息设置修改.txtSQL = "select* FROM [dbo].[库存信息表]"
frm库存信息设置修改.Show
ElseIf gint库存信息设置修改mode = 2 Then
Unload Me
frm库存信息设置修改.txtSQL = "select* FROM [dbo].[库存信息表]"
frm库存信息设置修改.Show
End If
End Sub
是显示不出来吗?