下面这个VB模块代码没错 但为什么改动数据库的表后运行 rst.Open TmpSQLstmt 这句就报错
发表在VB答疑区
2013-05-11
是否精华
是
否
版块置顶:
是
否
Public fMainForm As frmMain '定义主窗体类型
Public UserName As String '保存当前登录用户
Dim cnn As ADODB.Connection '数据库连接
Sub Main()
Dim fLogin As New frmLogin '定义登录窗口对象
App.Title = "体育馆设备管理系统" '应用程序标题
fLogin.Show vbModal '显示登录窗体
If Not fLogin.OK Then '如果登录不成功
End '结束应用程序
End If
Unload fLogin '卸载登录窗体
Set fMainForm = New frmMain '生成新的主窗体
fMainForm.Show '显示主窗体
End Sub
Public Function GenNo() As String '生成序号
GenNo = Format(Now, "yymmddhhmmss") '得到当前时间
Randomize
GenNo = GenNo & Int((99 - 10 + 1) * Rnd + 10) '生成随机序号
End Function
Private Sub Connect() '连接数据库
If IsConnect = True Then '如果连接标记为真,则返回。否则会出错
Exit Sub
End If
On Error GoTo DbOpenErr
Set cnn = New ADODB.Connection '关键New用于创建新对象cnn
With cnn
.Provider = "sqloledb"
.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=设备管理;Data Source=FNS34A9STD9CFAK\SQLEXPRESS"
.ConnectionTimeout = 10
.Open
End With
IsConnect = True '设置连接标记,表示已经连接到数据库
Exit Sub
DbOpenErr:
If Err = -2147467259 Then
Set cnn = Nothing
MsgBox "连接MS SQL Server数据库失败!" & vbCrLf & vbCrLf & "请检查配置是否完好,体育馆设备管理数据库是否存在?", vbOKOnly + vbInformation, "管理系统"
End
End If
End Sub
Public Sub Disconnect() '断开与数据库的连接
Dim rc As Long
If IsConnect = False Then Exit Sub '如果连接标记为假,标明已经断开连接,则直接返回
cnn.Close '关闭连接
Set cnn = Nothing
IsConnect = False
End Sub
Public Sub DB_Connect() '使用Connect_Num控制数据库连接
Connect_Num = Connect_Num + 1
Connect
End Sub
Public Sub DB_Disconnect()
If Connect_Num >= CONNECT_LOOP_MAX Then
Connect_Num = 0
Disconnect
End If
End Sub
Public Sub DBapi_Disconnect() '强制关闭api方式访问的数据库,计数器复位
Connect_Num = 0
Disconnect
End Sub
Public Sub SQLExt(ByVal TmpSQLstmt As String) '执行数据库操作语句
Dim cmd As New ADODB.Command '创建Command对象cmd
DB_Connect '连接到数据库
Set cmd.ActiveConnection = cnn '设置cmd的ActiveConnection属性,指定与其关联的数据库连接
cmd.CommandText = TmpSQLstmt '设置要执行的命令文本
cmd.Execute
Set cmd = Nothing
DB_Disconnect
End Sub
Public Function QueryExt(ByVal TmpSQLstmt As String) As ADODB.Recordset '执行数据库查询语句
Dim rst As New ADODB.Recordset
DB_Connect '连接到数据库
Set rst.ActiveConnection = cnn '设置rst的ActiveConnection属性,指定与其关联的数据库连接
rst.CursorType = adOpenKeyset
rst.LockType = adLockOptimistic '设置锁定类型
rst.Open TmpSQLstmt '打开记录集
Set QueryExt = rst '返回记录集
End Function
[font size=4][/font][font size=大小][/font][font size=1][/font][font size=6][/font][font face=楷体_GB2312][/font][font color=#FF0000][/font]
Public UserName As String '保存当前登录用户
Dim cnn As ADODB.Connection '数据库连接
Sub Main()
Dim fLogin As New frmLogin '定义登录窗口对象
App.Title = "体育馆设备管理系统" '应用程序标题
fLogin.Show vbModal '显示登录窗体
If Not fLogin.OK Then '如果登录不成功
End '结束应用程序
End If
Unload fLogin '卸载登录窗体
Set fMainForm = New frmMain '生成新的主窗体
fMainForm.Show '显示主窗体
End Sub
Public Function GenNo() As String '生成序号
GenNo = Format(Now, "yymmddhhmmss") '得到当前时间
Randomize
GenNo = GenNo & Int((99 - 10 + 1) * Rnd + 10) '生成随机序号
End Function
Private Sub Connect() '连接数据库
If IsConnect = True Then '如果连接标记为真,则返回。否则会出错
Exit Sub
End If
On Error GoTo DbOpenErr
Set cnn = New ADODB.Connection '关键New用于创建新对象cnn
With cnn
.Provider = "sqloledb"
.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=设备管理;Data Source=FNS34A9STD9CFAK\SQLEXPRESS"
.ConnectionTimeout = 10
.Open
End With
IsConnect = True '设置连接标记,表示已经连接到数据库
Exit Sub
DbOpenErr:
If Err = -2147467259 Then
Set cnn = Nothing
MsgBox "连接MS SQL Server数据库失败!" & vbCrLf & vbCrLf & "请检查配置是否完好,体育馆设备管理数据库是否存在?", vbOKOnly + vbInformation, "管理系统"
End
End If
End Sub
Public Sub Disconnect() '断开与数据库的连接
Dim rc As Long
If IsConnect = False Then Exit Sub '如果连接标记为假,标明已经断开连接,则直接返回
cnn.Close '关闭连接
Set cnn = Nothing
IsConnect = False
End Sub
Public Sub DB_Connect() '使用Connect_Num控制数据库连接
Connect_Num = Connect_Num + 1
Connect
End Sub
Public Sub DB_Disconnect()
If Connect_Num >= CONNECT_LOOP_MAX Then
Connect_Num = 0
Disconnect
End If
End Sub
Public Sub DBapi_Disconnect() '强制关闭api方式访问的数据库,计数器复位
Connect_Num = 0
Disconnect
End Sub
Public Sub SQLExt(ByVal TmpSQLstmt As String) '执行数据库操作语句
Dim cmd As New ADODB.Command '创建Command对象cmd
DB_Connect '连接到数据库
Set cmd.ActiveConnection = cnn '设置cmd的ActiveConnection属性,指定与其关联的数据库连接
cmd.CommandText = TmpSQLstmt '设置要执行的命令文本
cmd.Execute
Set cmd = Nothing
DB_Disconnect
End Sub
Public Function QueryExt(ByVal TmpSQLstmt As String) As ADODB.Recordset '执行数据库查询语句
Dim rst As New ADODB.Recordset
DB_Connect '连接到数据库
Set rst.ActiveConnection = cnn '设置rst的ActiveConnection属性,指定与其关联的数据库连接
rst.CursorType = adOpenKeyset
rst.LockType = adLockOptimistic '设置锁定类型
rst.Open TmpSQLstmt '打开记录集
Set QueryExt = rst '返回记录集
End Function
[font size=4][/font][font size=大小][/font][font size=1][/font][font size=6][/font][font face=楷体_GB2312][/font][font color=#FF0000][/font]