DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Help with VB code

1. Ana
Registered User
Join Date
Nov 2004
Location
Newark, New Jersey
Posts
38

## Help with VB code

I am doing matrix multiplication and connectivity.
I think that I got the multiplication working but I can't get the connectivity correct. PLease help me.
Here is the code in which I have trouble.

Private Sub cmdCalButton_Click()
p = 0
q = 0

'flag = ""

'Take elements of A in the array aay

For ro = 0 To 3

For co = 0 To 3

If A(q) = "" Then

aay(ro, co) = 0

Else

aay(ro, co) = Int(A(q))

End If

q = q + 1

Next

p = p + 4

q = p

Next

p = 0

q = 0

'Take all the elements of B in array bee

For ro = 0 To 3

For co = 0 To 3

If B(q) = "" Then

bee(ro, co) = 0

Else

bee(ro, co) = Int(B(q))

End If

q = q + 1

Next

p = p + 4

q = p

Next

'Take all the elements of C in array see

p = 0

q = 0

For ro = 0 To 3

For co = 0 To 3

If C(q) = "" Then

see(ro, co) = 0

Else

see(ro, co) = Int(C(q))

End If

q = q + 1

Next

p = p + 4

q = p

Next

Debug.Print "A***************"

For ro = 0 To 3 'Check for successful fill-up

For co = 0 To 3

Debug.Print "aay(" & ro & "," & co & ") = " & aay(ro, co)

Next

Next

Debug.Print "B***************"

For ro = 0 To 3 'Check for successful fill-up

For co = 0 To 3

Debug.Print "bee(" & ro & "," & co & ") = " & bee(ro, co)

Next

Next

Debug.Print "C********************"

Debug.Print "A***************"

For ro = 0 To 3 'Check for successful fill-up

For co = 0 To 3

Debug.Print "see(" & ro & "," & co & ") = " & see(ro, co)

Next

Next

'Processing logic
'Assign a dynamic matrix C

If Option1(11).Value = True Then

If (Right(cmbOrderOfA, 1) = Left(cmbOrderOfA, 1)) Then

For p = 0 To 3

For q = 0 To 3

temp(p, q) = 0

For ctr = 0 To 3

temp(p, q) = temp(p, q) + aay(p, ctr) * aay(ctr, q)

Next

Next

Next

If (Left(cmbOrderOfA, 1) = Right(cmbOrderOfA, 1)) Then

For p = 0 To 3

For q = 0 To 3

dee(p, q) = 0

For ctr = 0 To 3

dee(p, q) = dee(p, q) + temp(p, ctr) * aay(ctr, q)

Next

Next

Next

i = 0

For ro = 0 To 3

For co = 0 To 3

B(i).Text = dee(ro, co)

i = i + 1

Next

Next

Call All_Visible_B

Call All_Visible_B

ro = Int(Left((cmbOrderOfA.Text), 1))

co = Int(Right((cmbOrderOfA.Text), 1))

If (ro <> 4) Then

For i = (ro * 4) To 15

B(i).Visible = False

'D(i). = ""

Next

End If

q = co

If (co <> 4) Then

For i = co To 3

For p = 1 To 4

B(q).Visible = False

q = q + 4

Next

co = co + 1

Next
End If
End If
End If
End If

If Option1(0).Value = True Then

If (Right(cmbOrderOfA, 1) = Left(cmbOrderOfA, 1)) Then

For p = 0 To 3

For q = 0 To 3

temp(p, q) = 0

For ctr = 0 To 3

temp(p, q) = temp(p, q) + aay(p, ctr) * aay(ctr, q)

Next

Next

Next

If (Left(cmbOrderOfA, 1) = Right(cmbOrderOfA, 1)) Then

For p = 0 To 3

For q = 0 To 3

dee(p, q) = 0

For ctr = 0 To 3

dee(p, q) = dee(p, q) + temp(p, ctr) * aay(ctr, q) * aay(ctr, q)

Next

Next

Next

i = 0

For ro = 0 To 3

For co = 0 To 3

C(i).Text = dee(ro, co)

i = i + 1

Next

Next

Call All_Visible_c

Call All_Visible_c

ro = Int(Left((cmbOrderOfA.Text), 1))

co = Int(Right((cmbOrderOfA.Text), 1))

If (ro <> 4) Then

For i = (ro * 4) To 15

C(i).Visible = False

Next

End If

q = co

If (co <> 4) Then

For i = co To 3

For p = 1 To 4

C(q).Visible = False

q = q + 4

Next

co = co + 1

Next
End If
End If
End If
End If

If Option1(2).Value = True Then

If (Right(cmbOrderOfA, 1) = Left(cmbOrderOfB, 1)) Then

For p = 0 To 3

For q = 0 To 3

dee(p, q) = 0

For ctr = 0 To 3

dee(p, q) = dee(p, q) + temp(p, ctr) + aay(ctr, q) + bee(ctr, q)

Next

Next

Next

If (Left(cmbOrderOfC, 1) = Right(cmbOrderOfA, 1)) Then

For p = 0 To 3

For q = 0 To 3

dee(p, q) = 0

For ctr = 0 To 3

dee(p, q) = dee(p, q) + temp(p, ctr) + see(ctr, q)
Next

Next

Next

i = 0

For ro = 0 To 3

For co = 0 To 3

D(i).Caption = dee(ro, co)

i = i + 1

Next

Next

Call All_Visible_D

Call All_Visible_D

ro = Int(Left((cmbOrderOfA.Text), 1))

co = Int(Right((cmbOrderOfB.Text), 1))

If (ro <> 4) Then

For i = (ro * 4) To 15

D(i).Visible = False

Next

End If

q = co

If (co <> 4) Then

For i = co To 3

For p = 1 To 4

D(q).Visible = False

q = q + 4

Next

co = co + 1

Next

End If
End If
End If
End If

End Sub

Hope you can find what is wrong.

2. Registered User
Join Date
Apr 2005
Posts
9
if you can post a copy of this code that's indented, has all the var declarations, and has the extra subs, I'll look at it for you.

3. Ana
Registered User
Join Date
Nov 2004
Location
Newark, New Jersey
Posts
38

## Help(rest of the code)

Dim row, col, ctr, t, ro, co, q, p, i, m As Long

Dim roA, roB, roC, coA, coB, coC As Long

Dim aay(4, 4) As Long

Dim bee(4, 4) As Long

Dim see(4, 4) As Long

Dim dee(4, 4) As Long

Dim temp(4, 4) As Long

Dim flag As String

Option Explicit

Private Sub A_Change(Index As Integer)

If IsNumeric(A(Index).Text) = False And A(Index).Text <> "" Then

If (A(Index).Text <> "-") Then

i = MsgBox(A(Index).Text & " " & "is not a numeric value.Enter only numeric values", vbOKOnly + vbExclamation, "Non numeric input")

A(Index).SetFocus

End If

End If

End Sub

Private Sub A_LostFocus(Index As Integer)

If IsNumeric(A(Index).Text) = False And A(Index).Text <> "" Then

If (A(Index).Text <> "-") Then

i = MsgBox(A(Index).Text & " " & "is not a numeric value.Enter only numeric values", vbOKOnly + vbExclamation, "Non numeric input")

A(Index).SetFocus

End If

End If

End Sub

Private Sub B_Change(Index As Integer)

If IsNumeric(B(Index).Text) = False And B(Index).Text <> "" Then

If (B(Index).Text <> "-") Then

i = MsgBox(B(Index).Text & " " & "is not a numeric value.Enter only numeric values", vbOKOnly + vbExclamation, "Non numeric input")

B(Index).SetFocus

End If

End If

End Sub

Private Sub B_LostFocus(Index As Integer)

If IsNumeric(B(Index).Text) = False And B(Index).Text <> "" Then

If (B(Index).Text <> "-") Then

i = MsgBox(B(Index).Text & " " & "is not a numeric value.Enter only numeric values", vbOKOnly + vbExclamation, "Non numeric input")

B(Index).SetFocus

End If

End If

End Sub

Private Sub C_Change(Index As Integer)

If IsNumeric(C(Index).Text) = False And C(Index).Text <> "" Then

If (C(Index).Text <> "-") Then

i = MsgBox(C(Index).Text & " " & "is not a numeric value.Enter only numeric values", vbOKOnly + vbExclamation, "Non numeric input")

C(Index).SetFocus

End If

End If

End Sub

Private Sub C_LostFocus(Index As Integer)

If IsNumeric(C(Index).Text) = False And C(Index).Text <> "" Then

If (C(Index).Text <> "-") Then

i = MsgBox(C(Index).Text & " " & "is not a numeric value.Enter only numeric values", vbOKOnly + vbExclamation, "Non numeric input")

C(Index).SetFocus

End If

End If

End Sub

Private Sub cmbOrderOfA_Click()

Call setlabels

Call All_Visible_A

ro = Int(Left((cmbOrderOfA.Text), 1))

co = Int(Right((cmbOrderOfA.Text), 1))

If (ro <> 4) Then

For i = (ro * 4) To 15

A(i).Visible = False

A(i).Text = ""

Next

End If

q = co

If (co <> 4) Then

For i = co To 3

For p = 1 To 4

A(q).Visible = False

A(q).Text = ""

q = q + 4

Next

' q = co

co = co + 1

Next

End If

End Sub

Private Sub cmbOrderOfB_Click()

Call setlabels

Call All_Visible_B

ro = Int(Left((cmbOrderOfB.Text), 1))

co = Int(Right((cmbOrderOfB.Text), 1))

If (ro <> 4) Then

For i = (ro * 4) To 15

B(i).Visible = False

B(i).Text = ""

Next

End If

q = co

If (co <> 4) Then

For i = co To 3

For p = 1 To 4

B(q).Visible = False

B(q).Text = ""

q = q + 4

Next

' q = co

co = co + 1

Next

End If

End Sub

Private Sub cmbOrderOfC_Click()

Call setlabels

Call All_Visible_c

ro = Int(Left((cmbOrderOfC.Text), 1))

co = Int(Right((cmbOrderOfC.Text), 1))

If (ro <> 4) Then

For i = (ro * 4) To 15

C(i).Visible = False

C(i).Text = ""

Next

End If

q = co

If (co <> 4) Then

For i = co To 3

For p = 1 To 4

C(q).Visible = False

C(q).Text = ""

q = q + 4

Next

'q = co

co = co + 1

Next

End If

End Sub

Private Sub cmdExit_Click()
End
End Sub

Private Sub Command1_Click()

Call All_Visible_A

Call All_Visible_B

Call All_Visible_c

Call All_Visible_D

For i = 0 To 15

A(i).Text = ""

B(i).Text = ""

C(i).Text = ""

D(i).Caption = ""

Next

cmbOrderOfA.Text = cmbOrderOfA.List(15)

cmbOrderOfB.Text = cmbOrderOfB.List(15)

cmbOrderOfC.Text = cmbOrderOfC.List(15)

A(0).SetFocus

Label6.Caption = ""

'lblOrderD.Caption = ""

End Sub

Call setlabels

For i = 0 To 15

A(i).MaxLength = 3

B(i).MaxLength = 3

C(i).MaxLength = 3

Next

ctr = 1
'Initialize the combo-boxes

For row = 1 To 4

For col = 1 To 4

cmbOrderOfA.AddItem row & " x " & col

cmbOrderOfB.AddItem row & " x " & col

cmbOrderOfC.AddItem row & " x " & col

If (col = 4) Then

Exit For

End If

Next

Next

cmbOrderOfA.Text = cmbOrderOfA.List(15)

cmbOrderOfB.Text = cmbOrderOfB.List(15)

cmbOrderOfC.Text = cmbOrderOfC.List(15)

'Set the TabIndex

cmbOrderOfA.TabIndex = 48

cmbOrderOfB.TabIndex = 49

cmbOrderOfC.TabIndex = 50

For t = 0 To 15

A(t).TabIndex = t

B(t).TabIndex = t + 16

C(t).TabIndex = t + 32

Next

End Sub

Public Sub All_Visible_A()

For i = 0 To 15

A(i).Visible = True

Next

End Sub

Public Sub setlabels()

lblOrderA.Caption = cmbOrderOfA.Text

lblOrderB.Caption = cmbOrderOfB.Text

lblOrderC.Caption = cmbOrderOfC.Text

End Sub

Public Sub All_Visible_B()

For i = 0 To 15

B(i).Visible = True

Next

End Sub

Public Sub All_Visible_c()

For i = 0 To 15

C(i).Visible = True

Next

End Sub
Public Sub All_Visible_D()

For i = 0 To 15

D(i).Visible = True

Next

End Sub

4. Ana
Registered User
Join Date
Nov 2004
Location
Newark, New Jersey
Posts
38

## ana

That is all the code that I have, I hope you can understand it.

5. Please do not get offended, but did you debug your code?
Input a matrix with known numbers, and known results (made by hand). Then put a breakpoint in the click event, and step using F8, one line per time.
Marco
PS: next time include your code using [ code ] and [ /code ]
PPS:the line
Dim row, col, ctr, t, ro, co, q, p, i, m As Long
declare only m as Long, all the others are Variant.

6. Ana
Registered User
Join Date
Nov 2004
Location
Newark, New Jersey
Posts
38

## Ana

Is working but I don't get the results that I want, the ones I get are wrong
because what I am tryong to do is multiply A*A^2 and put result in matrix B
then multiply A*A^3 and put result in matrix C. Them what I want to do is get the connectivity(union, sum) of matrix A, B and C and put the result in matrix D.
The only one I got right is the multiplication A*A^2.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise

×