当前位置:编程学习 > VB >>

毕业设计遇到的问题:vb连接数据库出现问题

不是计算机专业的,不小心选了一个计算机专业的毕业设计。一个航空管理信息系统,在网上下到代码了。

运行时候出现这个提示:
Run-time error '91':

Object variable or With block variable not set

是哪出错了吗,是odbc配置错了吗?我创建的是文件dsn,实在是不懂


Attribute VB_Name = "Module1"
Public fMainForm As frmMain
Public gintSmode As Integer 'for service
Public gintPmode As Integer 'for plane
Public gintAmode As Integer 'for airline
Public gintTmode As Integer 'for customertype
Public gintCmode As Integer 'for customer
Public gintKmode As Integer 'for ticket
Sub Main()
    Dim fLogin As New frmLogin
    fLogin.Show vbModal
    If Not fLogin.OK Then
        'Login Failed so exit app
        End
    End If
    Unload fLogin


    Set fMainForm = New frmMain
    fMainForm.Show
End Sub

Public Function ConnectString() _
   As String
'returns a DB ConnectString
   ConnectString = "FileDSN=ticket.dsn;UID=sa;PWD="
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", _
      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
Public Sub EnterToTab(Keyasc As Integer)
    If Keyasc = 13 Then
        SendKeys "{TAB}"
    End If
End Sub
Public Function GetRkno() As String
    GetRkno = Format(Now, "yymmddhhmmss")
    Randomize
    GetRkno = GetRkno & Int((99 - 10 + 1) * Rnd + 10)
End Function
--------------------编程问答-------------------- 单步跟踪哪步有问题 --------------------编程问答-------------------- 创建的文件dsn测试了吗
首先先把这个问题解决了 --------------------编程问答--------------------

单步执行

看看问题出在哪里 --------------------编程问答-------------------- 单步执行看看
你的dsn测试过了么 --------------------编程问答-------------------- Run-time error '91':

Object variable or With block variable not set
这个错误的意思是有一个对象变量定义了,但是没有设置.

例如:
    Dim db As ADODB.Connection  '实际应该是Dim db As New ADODB.Connection
    Dim rec As New ADODB.Recordset
    db.Open "..."
就会出现91错误
--------------------编程问答-------------------- 创建好文件dsn后,出现这个错误

Run-time error '339':

Component 'MSFLXGRD.OCX' or one of its dependencies not correctly registered: a file is 
missing or invalid

急死我了,马上就要上交毕业设计了
--------------------编程问答-------------------- 在线等大家的帮忙呀。  好像是说什么没有注册,怎么注册呀? --------------------编程问答-------------------- 运行中打
regsvr32 msflxgrd.ocx

这样打的话要注意
msflxgrd.ocx
这个文件要在system32下哦

--------------------编程问答-------------------- 兄弟,教你一招!假设实用ACCESS数据库
实际上,连接数据库哪里有这么复杂嘛,最简单的办法就是实用ADODC1控件。如果用代码:
dim rs As New ADODB.Recordset
dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库名称.mdb;Persist Security Info=False"(打开数据库)
rs.open"表名称", conn, adOpenKeyset, adLockPessimistic(打开数据库里面的表)
短短四行代码,是不是比你的强啊?



--------------------编程问答-------------------- 忘记补充一句:你首先要引用ADO库才行啊,不然就找不到对象了!
补充:VB ,  数据库(包含打印,安装,报表)
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,