为什么定义public变量从一个过程到另一个过程就失效?

zl13 2004-11-27 11:28:11
Public theText As Variant
Public theValue As Variant
Dim Conn As Object
Dim Rs As Object
Const cnConnStr = "DSN=LocalServer;uid=sa;pwd=19821113;Database=pubs"
Const adCmdText = 1
Const RecordsAffected = 60

Private Sub clean_Click()

End Sub

Private Sub execute_Click()

End Sub

Private Sub regtype_Change()
Stop

Dim index As Integer
MsgBox regtype.ListIndex + 1
index = regtype.ListIndex + 1
theValue (index)
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set Conn = CreateObject("Adodb.Connection")
Conn.Open cnConnStr
CommandText = "Select * from authors"
Set Rs = Conn.execute(CommandText, RecordsAffected, adCmdText)
RowNum = 2
Stop
If ListIndex <> "" Or ListIndex <> 0 Then
MsgBox theValue(ListIndex)
Else
If Not Rs.EOF Then
While Not Rs.EOF
For Colnum = 0 To Rs.Fields.Count - 1
ActiveSheet.Cells(RowNum, 2) = Rs("au_lname").Value
theText = Rs("au_fname")
theValue = Rs("au_id")
regtype.AddItem Rs("au_lname")
Next
Rs.MoveNext
RowNum = RowNum + 1
Wend
End If
End If
End Sub

theValue和theText只要出了Worksheet_SelectionChange就会对象无效,各位高手又什么好办法么

...全文
178 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
starsoulxp 2004-12-01
  • 打赏
  • 举报
回复
写在模块里吧
行云边 2004-12-01
  • 打赏
  • 举报
回复
要不也可以 在模块内 写:
Global theText As Variant
Global theValue As Variant
flyingZFX 2004-12-01
  • 打赏
  • 举报
回复
把 Public 写在模块中。
行云边 2004-11-30
  • 打赏
  • 举报
回复
Public theText As String
Public theValue As String

或者

Static theText As Variant
Static theValue As Variant
行云边 2004-11-30
  • 打赏
  • 举报
回复
楼主的生日是 1982年11月13日?
saiko 2004-11-30
  • 打赏
  • 举报
回复
看到你的

theText = Rs("au_fname")
theValue = Rs("au_id")

你的字段是什么类型呀?
在定义时最好定义的和你的字段的类型一致啊!!!!!!!

lazygod 2004-11-28
  • 打赏
  • 举报
回复
不定义为Variant,指明其数据类型又会如何?
zl13 2004-11-27
  • 打赏
  • 举报
回复
以上就是源程序,就是在同一个窗口下
lxcc 2004-11-27
  • 打赏
  • 举报
回复
MsgBox theValue(ListIndex)

//theValue是数组吗?

同时要保证Worksheet_SelectionChange与theValue等在同一窗体代码内,除非在模块內定义
aohan 2004-11-27
  • 打赏
  • 举报
回复
你是不是在不同的窗口或模块中有这样的定义,不然应该不会的

2,503

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧