vb redim preserve

来源:百度知道 编辑:UC知道 时间:2024/06/17 07:15:10
Option Base 1
Dim studname() As String*4

Sub Form_Click()
ReDim studname(10)
Dim i%,temp as String*4
For i=1 to 10
temp$=InputBox$(“Enter Name:”)
studname(i)=temp$
Next i
End Sub

上述程序中数组只能够保存10个同学的姓名,如果现在还要再录入10个同学的姓名到该数组中去,不能将原有的10个同学的数据删除,该怎么办?请在Next i的后面再追加程序。

如果在录入之前知道所有同学的个数,这样可以定义个固定的数组,
比如我要录入30个那么就定义为:
Dim studname(30) As String*4
这里可以定义更大一点,以防止后来变化
Dim studname(100) As String*4 ,用不到的就放在那里好了,

录入的时候
For i=1 to 100
temp$=InputBox$(“Enter Name:”)
if trim(temp$)="" then
if msgbox("输入为空,是否结束录入?",vbokcancel)=vbok then exit for
else
studname(i)=temp$
end if
Next i
if i>100 then
msgbox "已经录入了100条记录,结束录入!"
end if

使用的时候判断一下
for i=1 to 100
if studname(i)<>"" then
...
...
end if
next

如果不能确定录入的数量,最好使用动态数组
Dim studname() As String, temp As String
Dim i As Integer
Dim inputOK As Boolean
i = 1
ReDim studname(1)
Do
temp$ = InputBox$("输入姓名(输入空内容结束):")
If Trim(temp$) = "" Then
inputOK = (MsgBox("输入为空,是否结束录入?&qu