Solved

Explain code written by another coder

Posted on 2014-10-18
14
88 Views
Last Modified: 2014-10-18
Ihave code written by another coder that i don't understand
Textmatrix requires a row and column
Example:
On this line how does Recap.CompanyName produce a column
FlxGrd(2).TextMatrix(i + 1, Recap.CompanyName) = .CompanyName

Form declaration
Private Type Recap_UDT
  CompanyName  As Byte
  Estimator    As Byte
  sDate        As Byte
  BidAmount    As Byte
  BidMaterial  As Byte
  Bid2Amount   As Byte
  Bid2Material As Byte
  sqs          As Byte
  BusYrs       As Byte
End Type

Private Recap As Recap_UDT
End Form declaration

Private Function Populate_Grid()'should have been a sub not function
Dim i As Integer
Dim j As Integer
  FlxGrd(2).Rows = RoofCompanyTotal + 1
    For i = 0 To RoofCompanyTotal - 1
      With RoofCompany(i)
        For j = 0 To FlxGrd(2).Cols - 1
          FlxGrd(2).TextMatrix(i + 1, j) = vbNullString
        Next j
        FlxGrd(2).TextMatrix(i + 1, Recap.CompanyName) = .CompanyName
        FlxGrd(2).TextMatrix(i + 1, Recap.Estimator) = .Estimator
        FlxGrd(2).TextMatrix(i + 1, Recap.sDate) = .sDate
        FlxGrd(2).TextMatrix(i + 1, Recap.BidAmount) = .BidAmount
        FlxGrd(2).TextMatrix(i + 1, Recap.BidMaterial) = .BidMaterial
        FlxGrd(2).TextMatrix(i + 1, Recap.Bid2Amount) = .Bid2Amount
        FlxGrd(2).TextMatrix(i + 1, Recap.Bid2Material) = .Bid2Material
        FlxGrd(2).TextMatrix(i + 1, Recap.sqs) = .BusYrs
        'FlxGrd(2).TextMatrix(i + 1, Recap.BusYrs) = .BusYrs
      End With
    Next
    SetGridSize FlxGrd(2)
End Function

Open in new window

0
Comment
Question by:isnoend2001
14 Comments
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40388825
There's a bug in the code. Instead ...
      End With
    Next
    SetGridSize FlxGrd(2)
End Function
End Function
**.... it should be ....**
      Next
    End With
    SetGridSize FlxGrd(2)
End Function
End Function 

Open in new window

0
 

Author Comment

by:isnoend2001
ID: 40388834
Thanks vb_elmar, i see, but that does not answer my question
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40388875
FlxGrd(2).TextMatrix(i + 1, Recap.CompanyName) = .CompanyName
I'm not sure what you're asking but TextMatrix has two parameters, Row and Column, both of which need to be numbers., so let me ask you a question. What gets loaded into Recap.CompanyName? If it's a number then there's a much better way of referring to the TextMatrix column..
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40388887
In code you have declared....

Private Recap As Recap_UDT
End Form declaration

So somewhere, before this function is called, Recap must be assigned data to it.  Recap.CompanyName will be whatever is assigned at that point, not in this function.
0
 

Author Comment

by:isnoend2001
ID: 40388925
don' understand i will post the project
Project-roof--update5-.zip
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40388929
Thanks. Also please try to restate your question since it isn't clear to me what it is that you don't understand.
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40388942
After looking at the project I see that Recap is filled with numbers by this code.

230         With Recap
240           .CompanyName = 0
250           .Estimator = 1
260           .sDate = 2
270           .BidAmount = 3
280           .BidMaterial = 4
290           .Bid2Amount = 5
300           .Bid2Material = 6
310           .Sqs = 7
320           .BusYrs = 8
330         End With

Open in new window

which is a poor way to do it. If you want to change it then please ask another question about it.

In any case your FlxGrd(2).TextMatrix(i + 1, Recap.CompanyName) = .CompanyName line now says
"The FlxGrd(2) cell with the column value equal to i+1 and row 0, is the value in RoffCompany.CompanyName"
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:isnoend2001
ID: 40388948
I dont understand how FlxGrd(2).TextMatrix(i + 1, Recap.CompanyName)
the column recap.companyname = a number
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40388957
I dont understand how FlxGrd(2).TextMatrix(i + 1, Recap.CompanyName)
the column recap.companyname = a number

FlxGrd(2).TextMatrix(i + 1, Recap.CompanyName) = .CompanyName

is the same as

FlxGrd(2).TextMatrix(i + 1, 0) = .CompanyName

because Recap.CompanyName is set to zero. And TextMatrix(i + 1, 0) are the coordinates of a cell.
0
 

Author Closing Comment

by:isnoend2001
ID: 40388986
Thanks now i see didn't notice the
230         With Recap
240           .CompanyName = 0
250           .Estimator = 1
260           .sDate = 2
270           .BidAmount = 3
280           .BidMaterial = 4
290           .Bid2Amount = 5
300           .Bid2Material = 6
310           .Sqs = 7
320           .BusYrs = 8
330         End With
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40389016
Essentially what Martin is saying is this.  The method TextMatrix, takes a Row and Column index value as it's parameters.  The row part is easy because it is determined by the i value in the for loop.  The second value, which represents the column, is controlled by the Recap variable.  So you identify the row and column (which equates to a cell) in the matrix that you want to update.

So lets put it all together:
Private Function Populate_Grid()
Dim i As Integer
Dim j As Integer
  ' Set the number of rows in the grid equal to the number of roof companies + 1 (the plus one is for the header row)
  FlxGrd(2).Rows = RoofCompanyTotal + 1
    ' Let's loop through the RoofCompanyTotal value (subtracting one to avert an index out of bounds exception)
    For i = 0 To RoofCompanyTotal - 1
      ' With the current, i, RoofCompany; the first one will be 0 and the last one will be the RoofCompanyTotal - 1.
      With RoofCompany(i)
        ' Let's loop through the columns in the row + 1 (this way we skip the header row) and set each columns text equal 
        ' to a null string
        For j = 0 To FlxGrd(2).Cols - 1
          FlxGrd(2).TextMatrix(i + 1, j) = vbNullString
        Next j
        ' Recap.CompanyName tells us to write text into the column that has an index of 0, the text will equal the 
        ' current roofing company CompanyName.  We already know that row we are dealing with.
        FlxGrd(2).TextMatrix(i + 1, Recap.CompanyName) = .CompanyName
        ' Recap.Estimator tells us to write text into the column that has an index of 1, the text will equal the 
        ' current roofing company Estimator.  We already know that row we are dealing with.
        FlxGrd(2).TextMatrix(i + 1, Recap.Estimator) = .Estimator
        ' Recap.sDate tells us to write text into the column that has an index of 2, the text will equal the 
        ' current roofing company sDate.  We already know that row we are dealing with.
        FlxGrd(2).TextMatrix(i + 1, Recap.sDate) = .sDate
        ' Recap.BidAmount tells us to write text into the column that has an index of 3, the text will equal the 
        ' current roofing company BidAmount.  We already know that row we are dealing with.
        FlxGrd(2).TextMatrix(i + 1, Recap.BidAmount) = .BidAmount
        ' Recap.BidMaterial tells us to write text into the column that has an index of 4, the text will equal the 
        ' current roofing company BidMaterial.  We already know that row we are dealing with.
        FlxGrd(2).TextMatrix(i + 1, Recap.BidMaterial) = .BidMaterial
        ' Recap.Bid2Amount tells us to write text into the column that has an index of 5, the text will equal the 
        ' current roofing company Bid2Amount.  We already know that row we are dealing with.
        FlxGrd(2).TextMatrix(i + 1, Recap.Bid2Amount) = .Bid2Amount
        ' Recap.Bid2Material tells us to write text into the column that has an index of 6, the text will equal the 
        ' current roofing company Bid2Material.  We already know that row we are dealing with.
        FlxGrd(2).TextMatrix(i + 1, Recap.Bid2Material) = .Bid2Material
        ' Recap.sqs tells us to write text into the column that has an index of 7, the text will equal the 
        ' current roofing company sqs.  We already know that row we are dealing with.
        FlxGrd(2).TextMatrix(i + 1, Recap.sqs) = .BusYrs
        ' This is commented out so the column, if it exists, should have a null string value.
        'FlxGrd(2).TextMatrix(i + 1, Recap.BusYrs) = .BusYrs
      End With
    Next
    SetGridSize FlxGrd(2)
End Function

Open in new window


-saige-
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40389378
I note in your code that in the following code and several other places you refer to Employee and m_Employees, neither of which are defined in the code you posted. Is a module missing?

Private Sub ListEmployees()
Dim emp As Employee
Dim txt As String

    For Each emp In m_Employees
        txt = txt & emp.LastName & ", " & _
            emp.FirstName & vbCrLf
    Next emp
    txtEmployees.Text = txt
End Sub

Open in new window

0
 

Author Comment

by:isnoend2001
ID: 40389497
Thanks MartinLiss, i think the variables are unused and left from my first attempt to write this  using a collection
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40389537
If that's the case then you should get rid of them. To locate all occurrences you should try and compile the project,
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now