I have been writing some VBA scripts to perform actions in PComm sessions.
For my latest one the process is to have 2 sessions open, both logged in to a different/separate (sub)system.
I'm trying to find out how to make VBA:
- Connect to 1 session
- Check if that session has specific values in specific coordinates (i can use the GetText command for this)
- If it is not the session, switch to the next open session and loop
- If it is the session, continue with the rest of the process (exit sub)
EDIT:
i've got the following code which helps determine if the correct subsystem is open.
Sub FindSession(FindPlatform As Platform)
Dim SessionLetter As String
Dim strSendToPlatform As String
Dim SL As Integer
Dim Timer As Date
Const DelayInterval = "00:01:00"
On Error GoTo Inicio
Inicio:
Select Case FindPlatform
Case 1
strSendToPlatform = "CARE"
Case 2
strSendToPlatform = "GlobStar"
Case 3
strSendToPlatform = "MainFrame"
Case 4
strSendToPlatform = "Citsob"
Case 5
strSendToPlatform = "Cas"
Case 6
strSendToPlatform = "IMS"
End Select
For SL = 1 To 7
If SL = 1 Then SessionLetter = "A"
If SL = 2 Then SessionLetter = "B"
If SL = 3 Then SessionLetter = "C"
If SL = 4 Then SessionLetter = "D"
If SL = 5 Then SessionLetter = "E"
If SL = 6 Then SessionLetter = "F"
If SL = 7 Then SessionLetter = "G"
Set sConnmgr = CreateObject("PCOMM.autECLConnmgr")
Set objwcc = CreateObject("PCOMM.autECLSession")
objwcc.SetConnectionByName SessionLetter
Select Case FindPlatform
Case 1 'CARE
If objwcc.autECLPS.GetText(1, 65, 3) = "AC2" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
Case 2 'GlobStar
If objwcc.autECLPS.GetText(1, 29, 9) = "GLOBESTAR" Or objwcc.autECLPS.GetText(1, 26, 9) = "GLOBESTAR" Or objwcc.autECLPS.GetText(1, 26, 8) = "AMERICAN" Or objwcc.autECLPS.GetText(1, 25, 8) = "AMERICAN" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
Case 3 'MainFrame
If objwcc.autECLPS.GetText(2, 4, 5) <> " " And objwcc.autECLPS.GetText(2, 4, 5) = "USS10" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
Case 4 'Citsob
If objwcc.autECLPS.GetText(1, 2, 8) = "FASBRSCR" Or objwcc.autECLPS.GetText(1, 2, 7) = "FASAUTH" Then
SessionNotFound = False
Do While GetString(1, 2, 8) <> "FASBRSCR"
SendString "", 1, 9, True
Loop
Exit Sub
Else
Set objwcc = Nothing
End If
Case 5 'Cas
If objwcc.autECLPS.GetText(3, 18, 18) = "CARDMEMBER SERVICE" Or objwcc.autECLPS.GetText(3, 22, 3) = "CUR" Or objwcc.autECLPS.GetText(2, 18, 18) = "CARDMEMBER SERVICE" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
Case 6 'IMS
If objwcc.autECLPS.GetText(1, 2, 5) = "WNSPC" Or objwcc.autECLPS.GetText(7, 24, 7) = "SPECIAL" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
End Select
Next SL
It does check all my open sessions to see if the correct system is open, but it doesn't select that session to work in that.
For example, i was logged into the 'CARE' system and 'Globstar' system, and had 'CARE' as the active window. I had the VBA look for 'Globstar', which it did find, but it then continued to send commands to the other open session; in 'CARE'
Can anybody help with how to do this?
Thanks!
Related
I'm trying to send serial data from Arduino and read it on Visual Basic. When I execute the code sometimes works and sometimes doesn't: throwing exception, System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Can you help me?
I'm new to Visual Basic, thanks.
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim TWSL, TWAL, THL, AoAL, WAL, PeL, RoilL, RyL, RydL As Integer
Dim TWS, TWA, TH, AoA, WA, Pe, Roil, Ry, Ryd, TWSResult, TWAResult, THResult, AoAResult, WAResult, PeResult, RoilResult, RyResult, RydResult As String
Dim StrSerialIn, StrSerialInRam As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToParent()
SerialPort1.PortName = "COM4"
SerialPort1.BaudRate = 9600
SerialPort1.Open()
Timer1.Start()
SerialPort1.Write(TrackBarAWA.Value & Chr(10))
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
StrSerialIn = SerialPort1.ReadExisting
Dim TB As New TextBox
TB.Multiline = True
TB.Text = StrSerialIn
If TB.Lines.Count > 0 Then
If TB.Lines(0) = "Failed to read" Then
Timer1.Stop()
SerialPort1.Close()
Return
End If
StrSerialInRam = TB.Lines(0).Substring(0, 2)
If StrSerialInRam = "A" Then
TWS = TB.Lines(0)
TWSL = TWS.Length
Else
TWS = TWS
End If
StrSerialInRam = TB.Lines(1).Substring(0, 3)
If StrSerialInRam = "B" Then
TWA = TB.Lines(1)
TWAL = TWA.Length
Else
TWA = TWA
End If
StrSerialInRam = TB.Lines(2).Substring(0, 3)
If StrSerialInRam = "C" Then
TH = TB.Lines(2)
THL = TH.Length
Else
TH = TH
End If
StrSerialInRam = TB.Lines(3).Substring(0, 2)
If StrSerialInRam = "D" Then
AoA = TB.Lines(3)
AoAL = AoA.Length
Else
AoA = AoA
End If
StrSerialInRam = TB.Lines(4).Substring(0, 1)
If StrSerialInRam = "E" Then
WA = TB.Lines(4)
WAL = WA.Length
Else
WA = WA
End If
StrSerialInRam = TB.Lines(5).Substring(0, 3)
If StrSerialInRam = "F" Then
Pe = TB.Lines(5)
PeL = Pe.Length
Else
Pe = Pe
End If
StrSerialInRam = TB.Lines(6).Substring(0, 3)
If StrSerialInRam = "G" Then
Roil = TB.Lines(6)
RoilL = Roil.Length
Else
Roil = Roil
End If
StrSerialInRam = TB.Lines(7).Substring(0, 3)
If StrSerialInRam = "H" Then
Ry = TB.Lines(7)
RyL = Ry.Length
Else
Ry = Ry
End If
StrSerialInRam = TB.Lines(8).Substring(0, 3)
If StrSerialInRam = "I" Then
Ryd = TB.Lines(8)
RydL = Ryd.Length
Else
Ryd = Ryd
End If
TWSResult = Mid(TWS, 2, TWSL)
TWAResult = Mid(TWA, 2, TWAL)
THResult = Mid(TH, 2, THL)
AoAResult = Mid(AoA, 2, AoAL)
WAResult = Mid(WA, 2, WAL)
PeResult = Mid(Pe, 2, PeL)
RoilResult = Mid(Roil, 2, RoilL)
RyResult = Mid(Ry, 2, RyL)
RydResult = Mid(Ryd, 2, RydL)
TWSvalue.Text = TWSResult
TWAvalue.Text = TWAResult
THvalue.Text = THResult
AoAvalue.Text = AoAResult
WAvalue.Text = WAResult
PeValue.Text = PeResult
RoilValue.Text = RoilResult
RyValue.Text = RyResult
RydValue.Text = RydResult
From what I gather, you are trying to get characters from specific places in specific lines on a text box. Based on the error message you included in your question, I assume the error occurs on a line of code containing the "String. Substring" method. If the string where you are getting the substring from is too short to cover the range you have specified in the substring method, you will get this error. For instance, if you are getting a substring that's 3 characters long from line 2 starting at character 0 and it has less than 3 characters, you will get this error.
See the documentation on the String.Substring method here
I am getting catastrophic error at certain sequence on listbox form selection...
how can I determine what exactly is causing catastrophic error? The error is repeatable from my file.
On bellow code I put msgbox "a" and error doesn't appear any more... If I remove that it will apear again. Also it appears only right after I run the excel file and open forms and select item on that listbox.
Also the code is around 4000 lines and 150000 characters for this specific form. With other classes it's over 10000 lines and over 300000 characters.
Private Sub ListBox3_Change()
Dim partf As Variant, ctype As String
ReDim fncleft(4)
ReDim fncright(4)
Dim i As Integer, h As Integer
For i = 0 To Me.ListBox3.ListCount - 1
If Me.ListBox3.Selected(i) = True Then
With bomfix.ExcelTables(Me.ComboBox_Tables2.Value).settings
If Me.ListBox3.List(i, 1) <> vbNullString Then
Me.CheckBox_SQLltrim.Enabled = True
Me.CheckBox_SQLrtrim.Enabled = True
Me.CheckBox_SQLtrim.Enabled = True
Me.CheckBox_SQLlower.Enabled = True
Me.CheckBox_SQLupper.Enabled = True
Me.CommandButtonSQLreplace.Enabled = True
Me.Frame9.Visible = True
Me.TextBox_ConstraintType.Visible = True
Me.CommandButton8.Visible = True
Me.Label99.Visible = True
Me.Label100.Visible = True
Me.TextBox_ConstrTableName.Visible = True
Me.TextBox_ConstrColumnName.Visible = True
If .TableExists(Me.ListBox3.List(i, 0)) = True Then
If .Table(Me.ListBox3.List(i, 0)).ColumnExists(Me.ListBox3.List(i, 1)) = True Then
Me.ComboBox_PartTable.List = .Tables
Me.ComboBox_PartTable.AddItem vbNullString, 0
With .Table(Me.ListBox3.List(i, 0))
Me.CheckBox_FullJoinTable.Enabled = True
Me.CheckBox_FullJoinTable.Value = .FullJoin
With .column(Me.ListBox3.List(i, 1))
Me.TextBox_ExportName.Value = .Value
Me.CheckBox_ColumnHidden.Value = .Hidden
Me.CheckBox_ColumnEnabled.Value = .Active
Me.TextBox_ConstraintType.Value = .ConstraintType
Me.TextBox_ConstrTableName.Value = .ConstraintTable
Me.TextBox_ConstrColumnName.Value = .ConstraintColumn
Me.CheckBox_SQLltrim.Value = .SQLltrim
Me.CheckBox_SQLrtrim.Value = .SQLrtrim
Me.CheckBox_SQLtrim.Value = .SQLtrim
Me.CheckBox_SQLlower.Value = .SQLlower
Me.CheckBox_SQLupper.Value = .SQLupper
MsgBox "b"
partf = .PartitionFilter
If Not UBound(partf) < 0 Then
Me.TextBox_PartResults.Value = partf(0)
If bomfix.ExcelTables(Me.ComboBox_Tables2.Value).settings.TableExists(partf(1)) = True Then
Me.ComboBox_PartTable.Value = partf(1)
Me.ComboBox_PartColumn.List = bomfix.ExcelTables(Me.ComboBox_Tables2.Value).settings.Table(Me.ComboBox_PartTable.Value).Columns
Me.ComboBox_PartColumn.AddItem vbNullString, 0
If bomfix.ExcelTables(Me.ComboBox_Tables2.Value).settings.Table(partf(1)).ColumnExists(partf(2)) = True Then
Me.ComboBox_PartColumn.Value = partf(2)
Me.ComboBox_PartOrderBy.Value = partf(3)
End If
End If
Else
Me.TextBox_PartResults.Value = vbNullString
Me.ComboBox_PartTable.Value = vbNullString
Me.ComboBox_PartColumn.Value = vbNullString
Me.ComboBox_PartOrderBy.Value = vbNullString
End If
Me.TextBox_FuncLeftCol.Value = Me.ListBox3.List(i, 0) & "." & Me.ListBox3.List(i, 1)
Me.ComboBox_FuncRightVal.Value = Me.ComboBox_FuncRightVal.List(0)
' add filters Array(and_or, Comparator, tableright, columnright, FunctionRight, FunctionLeft)
Me.ListBox4.Clear
For h = 1 To .FilterSet.Count
Me.ListBox4.AddItem
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 0) = .FilterSet(h)(0) ' and_or
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 1) = Join(.FilterSet(h)(5), "|") ' function left
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 2) = .FilterSet(h)(1) ' comparator
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 3) = .FilterSet(h)(2) ' table right
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 4) = .FilterSet(h)(3) ' column right
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 5) = Join(.FilterSet(h)(4), "|") ' function right
Next
End With
End With
End If
End If
ElseIf Me.ListBox3.List(i, 2) <> vbNullString Then
If .Aggregates.Exists(Me.ListBox3.List(i, 0)) = True Then
With .Aggregates(Me.ListBox3.List(i, 0))(1)
Me.CheckBox_SQLltrim.Enabled = False
Me.CheckBox_SQLrtrim.Enabled = False
Me.CheckBox_SQLtrim.Enabled = False
Me.CheckBox_SQLlower.Enabled = False
Me.CheckBox_SQLupper.Enabled = False
Me.CommandButtonSQLreplace.Enabled = False
Me.CheckBox_FullJoinTable.Enabled = False
Me.Frame9.Visible = False
Me.TextBox_ConstraintType.Visible = False
Me.CommandButton8.Visible = False
Me.Label99.Visible = False
Me.Label100.Visible = False
Me.TextBox_ConstrTableName.Visible = False
Me.TextBox_ConstrColumnName.Visible = False
Me.TextBox_ExportName.Value = .Value
Me.CheckBox_ColumnHidden.Value = .Hidden
Me.CheckBox_ColumnEnabled.Value = .Active
Me.TextBox_ConstraintType.Value = .ConstraintType
Me.TextBox_ConstrTableName.Value = .ConstraintTable
Me.TextBox_ConstrColumnName.Value = .ConstraintColumn
Me.TextBox_FuncLeftCol.Value = "aggregate"
Me.ComboBox_FuncRightVal.Value = Me.ComboBox_FuncRightVal.List(0)
' add filters Array(and_or, Comparator, tableright, columnright, FunctionRight, FunctionLeft)
Me.ListBox4.Clear
For h = 1 To .FilterSet.Count
Me.ListBox4.AddItem
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 0) = .FilterSet(h)(0)
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 1) = Join(.FilterSet(h)(5), "|")
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 2) = .FilterSet(h)(1)
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 3) = .FilterSet(h)(2)
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 4) = .FilterSet(h)(3)
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 5) = Join(.FilterSet(h)(4), "|")
Next
End With
End If
End If
End With
Exit For
End If
Next
Me.ComboBox_FuncTypeRight.Value = Me.ComboBox_FuncTypeRight.List(0)
Me.ComboBox_FuncTypeLeft.Value = Me.ComboBox_FuncTypeLeft.List(0)
Call ComboBox_FuncComp_Change
End Sub
For the first time when I run my window application it will successfully run, but when I change the database column and binding then it gives me an error that specific data column is not there in database; also I am make null chart datasource but it gives an error. Please help.
Dim ctrArr As Integer
Dim serCnt As Integer
Dim series1 As New Series
Dim seriesFound As Boolean = False
For serCnt = 0 To ctrChart.Series.Count - 1
ctrChart.Series(0).ArgumentDataMember = ""
ctrChart.Series(0).ValueDataMembers(0) = ""
ctrChart.Series(0).Visible = False
Next
For ctrArr = 0 To gStrYAxisParamArray.Length - 1 'deptname'
For serCnt = 0 To ctrChart.Series.Count - 1
If UCase(Trim(gStrYAxisParamArray(ctrArr))) = UCase(ctrChart.Series(serCnt).Name.ToString) Then
ctrChart.Series(serCnt).ArgumentDataMember = ""
ctrChart.Series(serCnt).ValueDataMembers(0) = ""
ctrChart.Series(serCnt).Visible = True
ctrChart.Series(serCnt).ArgumentDataMember = gxAxis
ctrChart.Series(serCnt).ValueDataMembers.Item(0) = Trim(gStrYAxisParamArray(ctrArr))
seriesFound = True
Exit For
End If
Next
If seriesFound = False Then
series1 = New Series(Trim(gStrYAxisParamArray(ctrArr)).ToString, ViewType.Bar)
'ctrChart.Series.AddRange(New Series() {series1, series2})
ctrChart.Series.Add(series1)
series1.ArgumentDataMember = ""
series1.ValueDataMembers(0) = ""
series1.Visible = True
series1.ArgumentDataMember = gxAxis
series1.Label.Border.Visible = False
series1.ValueDataMembers(0) = Trim(gStrYAxisParamArray(ctrArr))
series1.LegendText = Trim(gStrYAxisParamArray(ctrArr).ToString)
End If
seriesFound = False
Next
cmbSeries.Items.Clear()
For ctrArr = 0 To ChrtStockDept.Series.Count - 1
With cmbSeries.Items
cmbSeries.Items.Add(ChrtStockDept.Series(ctrArr).Name.ToString)
End With
Next
Any help would be great. I cant seem to get the ELSEIF with the AND to work.
I am using a user-form. When they press the button the user form appears with check boxes. I am having trouble when both check boxes are checked. individually they work fine,
I am not sure what i am doing wrong. any help would be greatly appreciated
If Intact.Value = True Then
storDate = Sheets("escalation").Cells(Selection.Row, 2)
storProject = Sheets("escalation").Cells(Selection.Row, 3)
StorBill = Sheets("escalation").Cells(Selection.Row, 4)
storIntact = "Intact"
With objWord.ActiveDocument
.formfields("text2").Result = storDate
.formfields("Text3").Result = storProject
.formfields("Text4").Result = StorBill
.formfields("Text9").Result = storIntact
End With
ElseIf Compugen.Value = True Then
storDate = Sheets("escalation").Cells(Selection.Row, 2)
storProject = Sheets("escalation").Cells(Selection.Row, 3)
StorBill = Sheets("escalation").Cells(Selection.Row, 4)
storCompugen = "Compugen"
With objWord.ActiveDocument
.formfields("text2").Result = storDate
.formfields("Text3").Result = storProject
.formfields("Text4").Result = StorBill
.formfields("Text9").Result = storCompugen
End With
ElseIf Intact.Value And Compugen.Value = True Then
storDate = Sheets("escalation").Cells(Selection.Row, 2)
storProject = Sheets("escalation").Cells(Selection.Row, 3)
StorBill = Sheets("escalation").Cells(Selection.Row, 4)
storIntact = "Intact"
storDate1 = Sheets("escalation").Cells(Selection.Row, 2)
storProject1 = Sheets("escalation").Cells(Selection.Row, 3)
StorBill1 = Sheets("escalation").Cells(Selection.Row, 4)
storCompugen = "Compugen"
With objWord.ActiveDocument
.formfields("text2").Result = storDate
.formfields("Text3").Result = storProject
.formfields("Text4").Result = StorBill
.formfields("Text9").Result = storIntact
.formfields("text5").Result = storDate1
.formfields("Text6").Result = storProject1
.formfields("Text7").Result = StorBill1
.formfields("Text8").Result = storCompugen
End With
End If
Thanks in advance
Try changing the order. Otherwise as soon as one condition is met the If clause is exited.
If Intact.Value And Compugen.Value Then
'code
ElseIf Intact.Value Then
'code
ElseIf Compugen.Value Then
'code
End If
If Intact.Value = True is true then the first, not the third block will run.
Similarly if Intact.Value = True is not true and Compugen.Value = True is true, then the second block will run.
So you can see that the third block is not reachable.
The solution is to put the Intact.Value = True And Compugen.Value = True case first in the group.
Finally, Foo.Value = True is a tautology of the simpler Foo.Value. You can drop all the explicit = True comparisons.
I am trying to figure out how to skip iterations on a For loop. I did some research and found that I could use Continue For, but that does not solve my issue. Here an example of what I want to do:
For i As Long = 1 to 7 Step 1
If (i= 2, 5 and 7) Then
'perform this action
Else
'perform other action.
End If
Next i
I worked out the following, but unfortunately it works for the <= 2 and the Else part of my loop and for the 5 and 7, performs the same action as what I asked to do on the Else part.
For i As Long = 1 To 7 Step 1
If (i <= 2 AndAlso 5 AndAlso 7) Then
strRange = ("A:D")
Else
strRange = ("A:A")
End If
xlRefSheets = ClientSheets(i)
With xlRefSheets
.Cells.EntireColumn.AutoFit()
.Range(strRange).EntireColumn.Hidden = True
End With
Next i
How about restating the if clause to
If (i <= 2) or (i = 5) or ( i =7) Then
...
So your code becomes:
For i As Long = 1 To 7 Step 1
If (i <= 2) OR (i = 5) OR (i = 7) Then
strRange = ("A:D")
Else
strRange = ("A:A")
End If
xlRefSheets = ClientSheets(i)
With xlRefSheets
.Cells.EntireColumn.AutoFit()
.Range(strRange).EntireColumn.Hidden = True
End With
Next i