标题:实时错误:对象名无效
只看楼主
落日幻影
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2013-10-10
结帖率:100%
已结贴  问题点数:20 回复次数:5 
实时错误:对象名无效
我是用VB连接SQL server的,说对象名'combo1'无效,这是我的代码:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rst As New ADODB.Recordset
Dim rct As New ADODB.Recordset

Private Sub Form_Load()
    Dim i As Integer
    cn.Open "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=database;Data Source=localhost"
    cn.CursorLocation = adUseClient
    rs.Open "select * from 中国", cn
    If rs.RecordCount <> 0 Then
        rs.MoveFirst
        For i = 0 To rs.RecordCount - 1
            Combo1.AddItem rs.Fields("省名")
            rs.MoveNext
        Next i
    End If
    rst.Open "select * from " & Trim(Combo1.Text) & "", cn
    If rst.RecordCount <> 0 Then
        rst.MoveFirst
        For i = 0 To rst.RecordCount - 1
            Combo2.AddItem rst.Fields("城市名")
            rst.MoveNext
        Next i
    End If
    rct.Open "select * from " & Trim(Combo2.Text) & "", cn
    If rct.RecordCount <> 0 Then
        rct.MoveFirst
        For i = 0 To rct.RecordCount - 1
            Combo3.AddItem rct.Fields("县名")
            rct.MoveNext
        Next i
    End If
    Combo1.Text = "选择省份"
    Combo2.Text = "选择城市"
    Combo3.Text = "选择县/区"
    Text1.Text = ""
End Sub

画了控件的:

这是错误截图:
搜索更多相关主题的帖子: localhost database server 中国 
2013-11-15 16:49
Artless
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:4211
专家分:28888
注 册:2009-4-8
得分:20 
这样较合理
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rst As New ADODB.Recordset
Dim rct As New ADODB.Recordset

Private Sub Form_Load()
    Dim i As Integer
    cn.Open "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=database;Data Source=localhost"
    cn.CursorLocation = adUseClient
    rs.Open "select * from 中国", cn
    If rs.RecordCount <> 0 Then
        rs.MoveFirst
        For i = 0 To rs.RecordCount - 1
            Combo1.AddItem rs.Fields("省名")
            rs.MoveNext
        Next i
    End If
    Combo1.Text = "选择省份"
    Combo2.Text = "选择城市"
    Combo3.Text = "选择县/区"
    Text1.Text = ""
End Sub

无知
2013-11-16 11:47
落日幻影
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2013-10-10
得分:0 
回复 楼主 落日幻影
error

[ 本帖最后由 落日幻影 于 2013-11-18 10:44 编辑 ]
2013-11-18 10:41
落日幻影
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2013-10-10
得分:0 
回复 2楼 Artless
那combo2,跟combo3的呢,我把combo1.text作为表名是不是写法有问题,我将combo1.text跟combo2.text替换为确切的表名时就没问题了
2013-11-18 10:43
落日幻影
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2013-10-10
得分:0 
回复 2楼 Artless
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rst As New ADODB.Recordset
Dim rct As New ADODB.Recordset

Private Sub Combo1_Change()
    Combo1.Refresh
    If Combo1.ListIndex > 0 Then
        rst.Open "select * from " & Trim(Combo1.Text) & "", cn
        If rst.RecordCount <> 0 Then
            rst.MoveFirst
            For i = 0 To rst.RecordCount - 1
                Combo2.AddItem rst.Fields("城市名")
                rst.MoveNext
            Next i
        End If
    End If
End Sub

Private Sub Combo2_Change()
    Combo2.Refresh
    If Combo2.ListIndex > 0 Then
        rct.Open "select * from " & Trim(Combo2.Text) & "", cn
        If rct.RecordCount <> 0 Then
            rct.MoveFirst
            For i = 0 To rct.RecordCount - 1
                Combo3.AddItem rct.Fields("县名")
                rct.MoveNext
            Next i
        End If
    End If
End Sub

Private Sub Command1_Click()
    Text1.Text = "地址为:" & Trim(Combo1.Text) & " " & Trim(Combo2.Text) & " " & Trim(Combo3.Text)
End Sub
Private Sub Form_Load()
    Dim i As Integer
    cn.Open "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=database;Data Source=localhost"
    cn.CursorLocation = adUseClient
    rs.Open "select * from 中国", cn
    If rs.RecordCount <> 0 Then
        rs.MoveFirst
        For i = 0 To rs.RecordCount - 1
            Combo1.AddItem rs.Fields("省名")
            rs.MoveNext
        Next i
    End If
    Combo1.Text = "选择省份"
    Combo2.Text = "选择城市"
    Combo3.Text = "选择县/区"
    Combo1.AddItem "选择省份", 0
    Combo2.AddItem "选择城市", 0
    Combo3.AddItem "选择县/区", 0
    Combo1.ListIndex = 0
    Combo2.ListIndex = 0
    Combo3.ListIndex = 0
    Text1.Text = ""
    Text1.Locked = True
End Sub
我现在改成这样,但是运行后combo1.text=""了:
2013-11-18 11:18
Artless
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:4211
专家分:28888
注 册:2009-4-8
得分:0 
combo1.text=江苏省

无知
2013-11-18 13:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-423574-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024777 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved