So my part of the code that gives me problems is this one. It says:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in WindowsApplication1.exe
Additional information: Index was outside the bounds of the array
for Label1.Text = question(i - 1,0) and I really don't understand.
I am at the start and I am really trying to learn things for programming.
Public Class Test1
Dim question(2, 5) As String
Dim i As Integer = 2
Private Sub Test1_Load()
question(1, 0) = "2+2="
question(1, 1) = "1"
question(1, 2) = "2"
question(1, 3) = "3"
question(1, 4) = "4"
question(2, 0) = "How old are you?"
question(2, 1) = "12"
question(2, 2) = "13"
question(2, 3) = "17"
question(2, 4) = "18"
Label1.Text = question(i - 1, 0)
nr1.Text = question(i - 1, 1)
nr2.Text = question(i - 1, 2)
nr3.Text = question(i - 1, 3)
nr4.Text = question(i - 1, 4)
End Sub
Your code didn't give me any errors on dotnetfiddle.net. So "question" is a 2D array indexed from 0-2 and 0-5. Here's what it kinda looks like:
0 1 2 3 4 5
0 s s s s s s
1 s s s s s s
2 s s s s s s
Where each 's' represents a string. So if you're accessing question(0, 0), then it would be the 's' in the top left. If you're accessing question(0, 1), then it would be the 's' to the right of that one. If you try to access something outside of the bounds of your array, you will get an error, for example, if you try to access question(3, 0).
So to fix your error, you need to figure out what the value of i is. Try putting
MessageBox.Show(i)
right before the line that's giving you the error.
Related
I'm using vba to fetch equipment numbers and their corresponding information and putting them in a listbox. A user will enter in the equipment number they want and excel will fetch the info. However, when I click my 'Get Data' button the first time it works ok. When i do it the second time for another equipment number I get the message "Could not set the List property. Invalid property array index." Here's my code:
Dim value As Long
Public i As Integer
Private Sub GetDataButton_Click()
Dim num As Variant
value = EquipmentNumber.value
For Each num In Sheets("S1 Cvtg Eqt List").Range(Range("B1"), Range("B1").End(xlDown))
If num = value Then
MWOList.AddItem (num)
MWOList.List(i, 1) = (num.Offset(0, 1))
MWOList.List(i, 2) = (num.Offset(0, 2))
MWOList.List(i, 3) = (num.Offset(0, 3))
MWOList.List(i, 4) = (num.Offset(0, 4))
MWOList.List(i, 5) = (num.Offset(0, 5))
i = i + 1
End If
Next num
i = i + 1
End Sub
Try below, please note that I had changed not only "i" declaration, and value to public, but also the List column position starts from 0, so if this is 6 element table, then switch it back.
The reason you had error was in fact another "i" iteration "i=i+1" after the loop, the list rows also start from 0, therefore you added 2nd index, and tried to insert it on the third position.
Public value As Long
Public i As Integer
Private Sub GetDataButton_Click()
Dim num As Variant
value = EquipmentNumber.value
For Each num In Sheets("S1 Cvtg Eqt List").Range(Range("B1"), Range("B1").End(xlDown))
If num = value Then
i = MWOList.ListCount 'set i to available space
MWOList.AddItem
MWOList.List(i, 0) = (num.Offset(0, 1))
MWOList.List(i, 1) = (num.Offset(0, 2))
MWOList.List(i, 2) = (num.Offset(0, 3))
MWOList.List(i, 3) = (num.Offset(0, 4))
MWOList.List(i, 4) = (num.Offset(0, 5))
End If
Next num
EquipmentNumber = ""
End Sub
For i = 0 To 2
If Niz1(i) > Niz2(i) Then
a = Niz1(i)
b = Niz2(i)
Call ZamjenaNiza(a, b)
Niz1(i) = Prvi
Niz2(i) = Drugi
End If
Next i
For j = 0 To 3
Me.DataGridView(j + 1, 1) = Niz1(j)
Me.DataGridView(j + 1, 2) = Niz2(j)
Next j
End Sub
Can anyone please help me with this problem? Can't find solution, not even on Visual Basic help page! It shows me error on this 2 code lines:
Me.DataGridView(j + 1, 1) = Niz1(j)
Me.DataGridView(j + 1, 2) = Niz2(j)
It says : Value of type 'Single' cannot be converted to 'System.Windows.Forms.DataGridViewCell'
Instead of trying to assign a Single value to a DataGridViewCell object, you should assign it to the .Value property of the object ..
Me.DataGridView(j + 1, 1).Value = Niz1(j)
I am trying to pull information from another sheet and then have it check to make sure 2 cells in this sheet are not the same. It is for a randomized meal plan, but on the same day you can not eat the same protein (to give you a little background). I have the code checking to see if it can be used during that meal and if it can be it is put into the next sheet. It does this for lunch and dinner. It is getting caught in the do - loop until loop and I am not sure how to solve this.
If you need anymore information I can provide screenshots if this does not make sense.
Do
RandNumLunch = Int((10 - 2 + 1) * Rnd + 2)
Do Until Sheet2.Cells(RandNumLunch, 6) = "Yes"
If Sheet2.Cells(RandNumLunch, 6) = "Yes" Then
Else
RandNumLunch = Int((10 - 2 + 1) * Rnd + 2)
End If
Loop
If Sheet2.Cells(RandNumLunch, 6) = "Yes" Then
Sheet3.Cells(3, 2) = Sheet2.Cells(RandNumLunch, 1)
End If
RandNumDinner = Int((10 - 2 + 1) * Rnd + 2)
Do Until Sheet2.Cells(RandNumDinner, 7) = "Yes"
If Sheet2.Cells(RandNumDinner, 7) = "Yes" Then
Else
RandNumDinner = Int((10 - 2 + 1) * Rnd + 2)
End If
Loop
If Sheet2.Cells(RandNumDinner, 7) = "Yes" Then
Sheet3.Cells(4, 2) = Sheet2.Cells(RandNumLunch, 1)
End If
Loop Until Sheet3.Cells(4, 2) <> Sheet3.Cells(3, 2)
In your final If..Else, you use RandNumLunch where you should be using RandNumDinner. This means that the whatever happens in between, you set Sheet3.Cells(3, 2) and Sheet3.Cells(4, 2) to the same value, so the final check can never be true.
I have a code which takes data from a PicoLog 1012 and records it into an excel spreadsheet. It is working well but currently it always records 12 channels of data. This will make it slow if a lot of data is required so I would like to allow users to enter a value in a cell to define the number of channels and then skip running unnecessary code based on this.
The important parts are:
Dim values() As Integer 'number of datapoints in the array. Equal to channels * number of datapoints required.
Dim numChannels As Integer
numChannels = Worksheets("Sheet1").Range("W5").value 'this allows the user to set the number of channels
Dim samplenum As Long
samplenum = Worksheets("Sheet1").Range("W3").value 'Reads number of samples desired per channel
nValues = samplenum * numChannels
'The code apparently requires one of these lines per channel.
channels(0) = 1
channels(1) = 2
channels(2) = 3
channels(3) = 4
channels(4) = 5
channels(5) = 6
channels(6) = 7
channels(7) = 8
channels(8) = 9
channels(9) = 10
channels(10) = 11
channels(11) = 12
ReDim values(12 * Worksheets("Sheet1").Range("W3").value) 'allow a variable data array
Dim sampleInterval As Long
Dim microsecs_for_block As Long
Dim testlength As Integer
testlength = Worksheets("Sheet1").Range("W4").value
microsecs_for_block = testlength * 1000000
status = pl1000SetInterval(handle, microsecs_for_block, nValues, channels(0), numChannels)
status = pl1000Run(handle, nValues, 0)
'If there is a more efficient way to do what follows then I would LOVE to hear it. Currently logging begins long after I activate the macro.
ready = 0
Do While ready = 0
status = pl1000Ready(handle, ready)
Loop
Cells(14, "P").value = "RECORDING COMPLETE" 'indicate readiness
' Get a block of W3 readings...
' we can call this routine repeatedly
' to get more blocks with the same settings
Dim triggerIndex As Long
Dim overflow As Integer
status = pl1000GetValues(handle, values(0), samplenum, overflow, triggerIndex)
' Copy the data into the spreadsheet
For i = 0 To samplenum - 1
1: Cells(i + 4, "A").value = adc_to_mv(values(numChannels * i + 0))
2: Cells(i + 4, "B").value = adc_to_mv(values(numChannels * i + 1))
3: Cells(i + 4, "C").value = adc_to_mv(values(numChannels * i + 2))
4: Cells(i + 4, "D").value = adc_to_mv(values(numChannels * i + 3))
5: Cells(i + 4, "E").value = adc_to_mv(values(numChannels * i + 4))
6: Cells(i + 4, "F").value = adc_to_mv(values(numChannels * i + 5))
7: Cells(i + 4, "G").value = adc_to_mv(values(numChannels * i + 6))
8: Cells(i + 4, "H").value = adc_to_mv(values(numChannels * i + 7))
9: Cells(i + 4, "I").value = adc_to_mv(values(numChannels * i + 8))
10: Cells(i + 4, "J").value = adc_to_mv(values(numChannels * i + 9))
11: Cells(i + 4, "K").value = adc_to_mv(values(numChannels * i + 10))
12: Cells(i + 4, "L").value = adc_to_mv(values(numChannels * i + 11))
Next i
My current idea is to write 12 different subs for this and call each one depending on the number of channels required but I am sure there must be an easier way?
Is there some sort of "skip" command which causes lines to be ignored?
IF numChannels = 2
Then skip 3,4,5,6,7,8,9,10,11,12
Else
IF numChannels = 3
Then skip 4,5,6,7,8,9,10,11,12
Else
IF'.... et cetera
I believe the code you are looking for is GoTo. You can have those if statements and if the if statement is triggered, it will "GoTo" where ever your tag has been placed
MSDN Example
Sub SkipLines()
Dim intSkipToLine as Integer
If intSkipToLine = 1 Then Goto Line1:
If intSkipToLine = 2 Then Goto Line2:
If intSkipToLine = 3 Then Goto Line3:
If intSkipToLine = 4 Then Goto Line4:
Line1:
' first line code
Line2:
' second line code
Line3:
' thrid line code
Line4:
' fourth line code
End Sub
You can see the final solution I came up with here
It is not a perfect solution for logging data with picolog but IMHO it is better than the proprietary software if you don't need huge datasets or real-time visualisation.
Pros:
Instantly usable by anyone with excel experience; no need to use
picolog’s interface
No need to convert data for processing into excel
Automatic graphing using excel’s interface
Scope for easy further development (multisheet handling, application-specific processing)
Cons:
No realtime visualisation of data
May struggle to handle logs with over 10,000 data points
i am getting EDI file as shown below,i need to process it ,and i need to maintain primary key ,foreign key also.
TH*4.2*857463*01**20091015*1045*P**~~IS*7564*ACME
PHARMACY~PHA*1234567890~PAT*MA*06*987544****SMITH*JOHN****1234 MAIN
ST**SOMEWHERE*MA*54356**19500101*M*01*01*INDIA**BURGER~
Here the column delimeter is * ,also if no value is provided they also put *.
i need to store fields from
TH*4.2*857463*01**20091015*1045*P**~~
into 1 table,by seperating fields.
So it would be
th01 th02 th03 th04 th05 th06 th07 th08 th09 th10
TH 4.2 857163 01 *(no value) 20091015 1045 p * (novalue) ~~
IS*7564*ACME PHARMACY into another table,and so on.
i cannot use third party tool as i cannot have xml files
Any help?
ok.
here is my vb.net code
Public Enum Segments
TH
PHA
PAT
IS1
End Enum
Dim arrLine As String()
Dim segmentcode As String
Dim counter As Integer
Dim linenumber As Integer = 1
Dim segmenetsequence As Hashtable = New Hashtable()
Dim setid As Guid = Guid.NewGuid()
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
arrLine = Row.LineText.Split("*"c)
segmentcode = SegmentValue(arrLine, 0)
Row.LineNumber = linenumber
Row.Setid = setid
counter = arrLine.Length
linenumber += 1
Select Case (segmentcode.ToUpper())
Case Segments.TH.ToString.ToUpper()
Row.TH01 = SegmentValue(arrLine, 1)
Row.TH02 = SegmentValue(arrLine, 2)
Row.TH03 = Convert.ToInt32(SegmentValue(arrLine, 3))
Row.TH04 = SegmentValue(arrLine, 4)
Row.TH05 = Convert.ToDateTime(SegmentValue(arrLine, 5))
Row.TH06 = SegmentValue(arrLine, 6)
Row.TH07 = SegmentValue(arrLine, 7)
Row.TH08 = Convert.ToInt32(SegmentValue(arrLine, 8))
Row.TH09 = SegmentValue(arrLine, 9)
Case Segments.IS1.ToString.ToUpper()
Row.IS01 = SegmentValue(arrLine, 1)
Row.IS02 = SegmentValue(arrLine, 2)
Row.IS03 = SegmentValue(arrLine, 3)
Case Segments.PHA.ToString.ToUpper()
Row.PHA01 = SegmentValue(arrLine, 1)
Row.PHA02 = SegmentValue(arrLine, 2)
Row.PHA03 = SegmentValue(arrLine, 3)
Row.PHA04 = SegmentValue(arrLine, 4)
Row.PHA05 = SegmentValue(arrLine, 5)
Row.PHA06 = SegmentValue(arrLine, 6)
Row.PHA07 = SegmentValue(arrLine, 7)
Row.PHA08 = SegmentValue(arrLine, 8)
Row.PHA09 = SegmentValue(arrLine, 9)
Row.PHA10 = SegmentValue(arrLine, 10)
Row.PHA11 = SegmentValue(arrLine, 11)
Row.PHA12 = SegmentValue(arrLine, 12)
Case Segments.PAT.ToString.ToUpper()
Row.PAT01 = SegmentValue(arrLine, 1)
Row.PAT02 = SegmentValue(arrLine, 2)
Row.PAT03 = SegmentValue(arrLine, 3)
Row.PAT04 = SegmentValue(arrLine, 4)
Row.PAT05 = Convert.ToInt32(SegmentValue(arrLine, 5))
Row.PAT06 = SegmentValue(arrLine, 6)
Row.PAT07 = SegmentValue(arrLine, 7)
Row.PAT08 = SegmentValue(arrLine, 8)
Row.PAT09 = SegmentValue(arrLine, 9)
Row.PAT10 = SegmentValue(arrLine, 10)
Row.PAT11 = SegmentValue(arrLine, 11)
Row.PAT12 = SegmentValue(arrLine, 12)
Row.PAT13 = SegmentValue(arrLine, 13)
Row.PAT14 = SegmentValue(arrLine, 14)
Row.PAT15 = SegmentValue(arrLine, 15)
Row.PAT16 = SegmentValue(arrLine, 16)
Row.PAT17 = SegmentValue(arrLine, 17)
Row.PAT18 = Convert.ToDateTime(SegmentValue(arrLine, 18))
Row.PAT19 = SegmentValue(arrLine, 19)
Row.PAT20 = Convert.ToInt32(SegmentValue(arrLine, 20))
Row.PAT21 = Convert.ToInt32(SegmentValue(arrLine, 21))
Row.PAT22 = SegmentValue(arrLine, 22)
Row.PAT23 = SegmentValue(arrLine, 23)
Row.PAT24 = SegmentValue(arrLine, 24)
End Select
End Sub
Public Function SegmentValue(ByRef LineArray As String(), ByVal Counter As Integer) As String
Throw New NotImplementedException
If LineArray.Length > Counter Then
Return LineArray(Counter).ToString().Trim()
End If
Return String.Empty
End Function
End Class
Speaking in broad terms, I would look at using a Script Component as a source. It would have an output collection per "thing" the file could contain. In your example, you'd have Output 0 with sufficient columns to describe the th rows above. You'd then have an Output 1 collection that describes the IS data set. You'll also need to factor in any keys, possibly surrogate keys generated within the source component to keep track of your data.
Once you've defined the all the columns in the collections, then it's a simple matter of writing the parsing logic in C#/VB.NET. Once parsed, you simply assign values into those collections.
Output0Buffer.AddRow();
// use this to assign a value
Output0Buffer.MyColumn = parsedValue;
// Use this for handling a null value
Output0Buffer.MyColumn_IsNull = true;
Now you can run the package and parsed data flows down the stream.
It sounds like you have foreign keys to be satisfied and surrogate values to be generated. If that is the case, I'd write most, if not all this data into a variety of staging tables and then proceed with validation and backfilling of data through repeated queries via Execute SQL Tasks chained to the Data Flow Task.
Need more detail? Edit your question and provide some yourself. We're all happy to give advice and direction but we are not in your cube and do not understand your needs.