How to declare a control array in Visual Basic 6?
I am getting an error control array element x does not exist (which means array index out of bounds).
If I know how to declare a control array then its easy to find why I am getting the error. Are control arrays in Visual Basic 6 a dynamic array?
You cannot do it purely in code.
To add a control array of buttons add a CommandButton to the form as normal, name it Test and change its index property to 0.
This gives you a control array containing Test(0), to add to the array at runtime use Load:
Dim i As Long
i = Test.UBound + 1
Load Test(i)
With Test(i)
.Move 10, 10, 500, 500
.Visible = True
End With
Related
I have a fairly complex look of code where I am looking through multiple control variables.
I am getting an error 'Invalid 'for' loop control variable
the line in questions is
for w(1) = 32 to 127
I am more familiar with VBA where I would have zero problem with this statement.
I'm guessing it has something to do with the fact that i will be looping through w(1),w(2),w(3) etc. in the same tree. I initialize the variable as dim x(10) but have also tried dim w() , dim w() redim w(10)
Any thoughts? its a fairly critical aspect of the script; as such I am unwilling to swap out all my w 1,2... for individual variables
Thoughts?
EDIT:
As per comments I should clarify a Few things:
Essentially there is a alpha numeric association with an ID in a system that I am working with which I was not handed down the key too. So I have a multi-dimensional array of rates that are used for multiplying out costs.
What I am doing is working backwards through invoices and matching a material with very subtle differences that have different pricings.
For simplicity sake, say theres a 2 dimensional material where AA, AB, ... A9 are all priced through several multiplication factors in what would just be a 2x2 grid. So maintaining a pivot point based on the position in string is very important. For this code you could take tier to mean how many characters in the string (aka how complex the composition of the material):
dim x(), w()
for tier = 1 to 2
for w(1) = 32 to 127
x(1)= chr(w(1))
If tier = 2 then
for w(2)= 32 to 127
X(2)=chr(w(2))
next
end if
str = ""
for y = 1 to (tier)
str = trim(str & x(y))
next
'''msgbox str 'debug
next
end if
str = ""
for y = 1 to (tier)
str = trim(str & x(y))
next
'' msgbox str ' debug
next 'tier
This is just an excerpt i pulled to get a basic idea of the structure w/o any calculations. this is in essence what is not working
The error is quite clear, you cannot use an Array as the control variable. The definition in For...Next Statement is even clearer;
Numeric variable used as a loop counter. The variable cannot be an array element or an element of a user-defined type.
This is one of the key differences between VBA and VBScript.
You won't loop through x(1),x(2)...on what you write it's going like this 32(1),33(1)....what type it's your w(1) and how you define him?
While reviewing a very old VB6 working code I get a very strange statement.
aryValue = aryPersons(8, i)
Where aryValue and aryPersons are multidimensional array and declared as
dim aryPersons, aryValue
Anyone having any idea what is does?
I tried the same in test application but it is giving Type mismatch (Error 13)
ANSWER:
It is my bad to understand the VB code as I was expecting it will be strongly data type language. Actually at aryPersons(8, i) a two dimension array were getting stored and while fetching it gives use a 2D array data that can be easily assigned to aryValue as it is also a 2D array.
It is strange to me that in 2D array at any position you store a any kind of data even another 2D data.
It seems likely that aryStepPersonOptions has an array as its value:
Dim SomeArray(8, 8) As String
Dim aryStepPersonOptions, aryValue
Dim i As Long
SomeArray(8, 8) = "Hello"
aryStepPersonOptions = SomeArray
i = 8
aryValue = aryStepPersonOptions(8, i)
MsgBox aryValue
Of course the pseudo-hungarian ary prefix used seems to do more to add confusion than otherwise. Sadly far too much code contains cargo-culted messes like this. Lets hope nobody copy/pastes my throwaway example SomeArray too.
Given a resource file, containing a combo box definition, for a C++ MFC program, is there a way to programmatically obtain the option strings?
When defining a dialog in the Visual Studio resource editor, one can specify the options with a ;-delimited string. Where are these strings then stored? I understand as well that one can programmatically add strings to the dialog box during dialog init, obtaining them is another story.
Nevertheless, my problem is that I don't have access to the dialog object, neither is it visible at the time I wish to obtain the option strings. Is that even possible?
You can create a member variable for combobox or
CComboBox* pBoxOne;
pBoxOne = (CComboBox*) GetDlgItem(IDC_COMBO1);
CString str, str2;
int n;
for (int i=0;i < pBoxOne->GetCount();i++)
{
n = pBoxOne->GetLBTextLen( i );
pBoxOne->GetLBText( i, str.GetBuffer(n) );
str.ReleaseBuffer();
str2.Format(_T("item %d: %s\r\n"), i, str.GetBuffer(0));
afxDump << str2;
}
The option strings are stored in the resource file itself. I have added options as 1;2;3 and the resource file entries are
IDD_MFC_DIALOG_DIALOG DLGINIT
BEGIN
IDC_COMBO1, 0x403, 2, 0
0x0031,
IDC_COMBO1, 0x403, 2, 0
0x0032,
IDC_COMBO1, 0x403, 2, 0
0x0033
END
I am working in a VB6 maintenance application. It is a windows based application. My client wants to configure the controls tab index at runtime. I am saving the client setting to the access database.
The following sub sets the tab index of the controls
Private Sub SetTabSetting()
Dim i As Integer
Dim Ctr As Control
If UBound(TSetting) > 0 Then
For i = 0 To UBound(TSetting)
For Each Ctr In Me.Controls
Dim matched As Boolean: matched = False
If Ctr.Name = TSetting(i).ControlName Then
Ctr.TabIndex = TSetting(i).TabIndexNum
Exit For
End If
Next
Next
End If
End Sub
TSetting is a TYPE Array defined in a Global Module.
Private Sub Form_Load()
GetRATabSetting
SetRATabSetting
End Sub
GetRATabSetting is extracting the values from the database and populating into the TYPE arrray.
The code is getting executed quite fine. Even the values get extracted from the database and set to the controls correctly. But the tab is following the index what is set in the designtime.
Am I doing any mistake? Is it possible to set the tabindex of the controls at runtime ? Is there any other way to perform this ?
Suppose you have 5 controls on a form and their tab order is like this
Index - TabIndex
1 - 0
2 - 1
3 - 2
4 - 3
5 - 4
If you change 3 to 1 Then it will look like this
Index - TabIndex
1 - 0
2 - 2
3 - 1
4 - 3
5 - 4
Visual Basic will automatically bump up by one all tabindex equal to and higher than the one you assigned. There will never be a time where two controls have the same tabindex. This causes problems for routines that assign tab indexes like yours.
What you should do is not assign the tabindex directly from the database but rather build an array of control indexes associated with tab indexes. Sort it based on the tabindex and then start assigning, starting at whatever is at tabindex 0 (or the lowest).
What is the "best" way to determine the number of elements in an array in VBScript?
UBound() tells you how many slots have been allocated for the array, but not how many are filled--depending on the situation, those may or may not be the same numbers.
First off there is no predefined identifier called vbUndefined as the currently accepted answer appears to imply. That code only works when there is not an Option Explicit at the top of the script. If you are not yet using Option Explicit then start doing so, it will save you all manner of grief.
The value you could use in place of vbUndefined is Empty, e.g.,:-
If arr(x) = Empty Then ...
Empty is a predefined identify and is the default value of a variable or array element that has not yet had a value assigned to it.
However there is Gotcha to watch out for. The following statements all display true:-
MsgBox 0 = Empty
MsgBox "" = Empty
MsgBox CDate("30 Dec 1899") = True
Hence if you expect any of these values to be a valid defined value of an array element then comparing to Empty doesn't cut it.
If you really want to be sure that the element is truely "undefined" that is "empty" use the IsEmpty function:-
If IsEmpty(arr(x)) Then
IsEmpty will only return true if the parameter it actually properly Empty.
There is also another issue, Null is a possible value that can be held in an array or variable. However:-
MsgBox Null = Empty
Is a runtime error, "invalid use of null" and :-
MsgBox IsEmpty(Null)
is false. So you need to decide if Null means undefined or is a valid value. If Null also means undefined you need your If statement to look like:-
If IsEmpty(arr(x)) Or IsNull(arr(x)) Then ....
You might be able to waive this if you know a Null will never be assigned into the array.
I'm not aware of a non-iterative method to do this, so here's the iterative method:
Function countEmptySlots(arr)
Dim x, c
c = 0
For x = 0 To ubound(arr)
If arr(x) = vbUndefined Then c = c + 1
Next
countEmptySlots = c
End Function
As Spencer Ruport says, it's probably better to keep track yourself to begin with.
There's nothing built in to tell you what elements are filled. The best way is to keep track of this yourself using a count variable as you add/remove elements from the array.