How can I improve this iterative function? - vb.net

I am trying to simplify this function which is similar from
Function Snake_makestep()
maincar.Location = locate(xpos, ypos)
car0.Location = locate(posx(0), posy(0))
car1.Location = locate(posx(1), posy(1))
If car2.Visible = True Then
car2.Location = locate(posx(2), posy(2))
End If
If car3.Visible = True Then
car3.Location = locate(posx(3), posy(3))
End If
If car4.Visible = True Then
car4.Location = locate(posx(4), posy(4))
End If
To
If car30.Visible = True Then
car30.Location = locate(posx(30), posy(30))
End If
End Function
I'm not sure If I can/how to use Controls.Find solution within this function. Any help?

To answer the question as asked:
For i = 2 To 30
Dim car = Controls("car" & i)
If car.Visible Then
car.Location = locate(posx(i), posy(i))
End If
Next
Visible and Location are both members of the Control class so it doesn't matter what type of control those car variables are.
Note that this assumes that all controls are on the form itself. If they are in a different parent container, you'd need to use the Controls collection of that container.
Also, I have started i at 2 there, so you'd still need the explicit code for car0 and car1. If they are always visible then you could start the loop at 0 and it would still work, saving you those two lines of code as well.

Related

If combo box contains text like multiple criteria then disable text boxes below

I'd like to disable text boxes if the SponName combo box contains certain company names. I thought I could try a for loop for this, but I'm not sure if I created the right variables or used the for loop properly. Please help!
I've tried cobbling together the following code from the threads below but can't quite adapt it to my problem. I'm still learning VBA and am not great with declaring variables. There's no error message, but the text fields just won't disable after updating SponName.
VBA code for if like multiple criteria
Private Sub SponName_AfterUpdate()
Dim sponcontains As Variant
sponcontains = SponName.Text
Criteria = Array("Company1*", "Company2*", "Company3*", "Company24*")
For i = LBound(Criteria) To UBound(Criteria)
If InStr(sponcontains, Criteria(i)) > 0 Then
cps_manufsite_name.Enabled = False
cps_manufsite_city.Enabled = False
cps_manufsite_st.Enabled = False
cps_manufsite_ctry.Enabled = False
Else
cps_manufsite_name.Enabled = True
cps_manufsite_city.Enabled = True
cps_manufsite_st.Enabled = True
cps_manufsite_ctry.Enabled = True
End If
Next i
End Sub
Use Text property only when control still has focus. You need Value property which is default for data controls so don't have to reference. Explicitly declare all variables. Should have Option Explicit at top of all modules. Set the VBA editor > Tools > Options > Require Variable Declaration to automatically add to new modules - will have to type into existing. Use of * wildcard not appropriate with InStr() function - it's just another literal character.
Like this - default value for Boolean variable is False:
Option Compare Database
Option Explicit
___________________________________________________________________________________
Private Sub SponName_AfterUpdate()
Dim sponcontains As Variant, criteria As Variant, booE As Boolean, i As Integer
sponcontains = Me.SponName
criteria = Array("Company1", "Company2", "Company3", "Company24")
For i = LBound(criteria) To UBound(criteria)
If InStr(sponcontains, criteria(i)) > 0 Then
booE = True
Exit For
End If
Next
cps_manufsite_name.Enabled = Not booE
cps_manufsite_city.Enabled = Not booE
cps_manufsite_st.Enabled = Not booE
cps_manufsite_ctry.Enabled = Not booE
End Sub
But your solution without loop is just as valid and most likely shorter. Again, use Boolean variable to set Enabled state instead of duplicating.
booE = InStr("Company1,Company2,Company3,Company24", Me.SponName) > 0
Consider what would happen if you had to modify this list. Would have to modify code and distribute new db version to users. Alternatively, use a table with an attribute (can be a yes/no field) for company status. Code can do a lookup to table. Better to use company ID value to search. Your combobox should have that as a column in its RowSource and usually that would be the combobox's value.
booE = DLookup("Status", "Companies", "CompanyID = " & Me.SponName)
Use of Conditional Formatting can often avoid VBA altogether.

How can I use iteration to make this vbnet code more efficient?

Function set_bar_positions()
bar_x(0) = delocateX(bar1.Left)
bar_y(0) = delocateY(bar1.Top)
bar_x(1) = delocateX(bar2.Left)
bar_y(1) = delocateY(bar2.Top)
bar_x(2) = delocateX(bar3.Left)
bar_y(2) = delocateY(bar3.Top)
This snippet from one of my functions show what I'm trying to do. These lines repeat almost identical until the end of the function where this is called:
bar_x(29) = delocateX(bar30.Left)
bar_y(29) = delocateY(bar30.Top)
I have tried iterating this functions by doing stuff like this, but now I know I can't:
Dim num As Integer = 0
bar_x(num) = delocateX(bar(num)).Left)
I am trying to make this code more efficient and have less lines. Anyone have an idea I can implement?
You can use Controls.Find
For i = 0 To 29
Dim cs = Me.Controls.Find("bar" & i.ToString(), True)
If cs.Any() Then
Dim c = cs.First()
bar_x(i) = delocateX(c.Left)
bar_y(i) = delocateY(c.Top)
End If
Next

ms-access shorten vba code

I'm using the following code alot in my project:
txtVoornaam.Locked = False
txtVoornaam.BorderStyle = 4
txtVoornaam.BorderColor = RGB(255, 165, 0
txtAchternaam.Locked = False
txtAchternaam.BorderStyle = 4
txtAchternaam.BorderColor = RGB(255, 165, 0)
txtAfdeling.Locked = False
txtAfdeling.BorderStyle = 4
txtAfdeling.BorderColor = RGB(255, 165, 0)
I wonder if there is a way to not display this in my code or shorten this. The code gets very long if i use this a few times..
Whenever you need to repeat a set of instructions, instead of copy+pasta'ing code your first reaction should be to ask yourself "how can I avoid copying this chunk over and over?" - and the solution is pretty much always to extract a method and parameterize it.
So you take one of the repeated chunks:
txtAchternaam.Locked = False
txtAchternaam.BorderStyle = 4
txtAchternaam.BorderColor = RGB(255, 165, 0)
and then copy it one last time in a new scope:
Private Sub RenameMe()
txtAchternaam.Locked = False
txtAchternaam.BorderStyle = 4
txtAchternaam.BorderColor = RGB(255, 165, 0)
End Sub
Then you extract the parameters:
Private Sub RenameMe(ByVal target As Control)
target.Locked = False
target.BorderStyle = 4
target.BorderColor = RGB(255, 165, 0)
End Sub
Then you replace the repeated chunks with calls to that new procedure:
RenameMe txtVoornaam
RenameMe txtAchternaam
RenameMe txtAfdeling
Or if that's still tedious you can iterate controls and call that procedure in the loop body - whatever works best for you.
And if you need more flexibility, extract more parameters and make them Optional as needed:
Private Sub RenameMe(ByVal target As Control, Optional ByVal lockCtrl As Boolean = False, Optional ByVal brdrStyle As Long = 4, Optional ByVal brdrColor As Long = 42495)
target.Locked = lockCtrl
target.BorderStyle = brdrStyle
target.BorderColor = brdrColor
End Sub
Now the hard part is to give RenameMe a meaningful name that properly conveys what's going on here. I'd suggest FormatControl or something along these lines.
An option if you have several controls that you are creating through a form would be to do the following:
Dim names() As String
names = Split("txtVoornaam,txtAchternaam,txtAfdeling", ",")
Dim ctrl As Variant
Dim ctrlName As Variant
For Each ctrl In Me.Controls
For Each ctrlName In names
If StrComp(ctrlName, ctrl.Name) = 0 Then
ctrl.Locked = False
ctrl.BorderStyle = 4
ctrl.BorderColor = RGB(255, 165, 0)
End If
Next ctrlName
Next ctrl
This code iterates through each of the control names that fit your list.
However, this is much less efficient than Mat's Mug's answer because you are iterating through the entire list of controls in your form, but it does showcase how you might take a list of static names and iterate through them and a collection.
If you wanted to change all the text controls this would be the way to do it; simply remove the ctrlName check.
As Parfait has correctly pointed out, you could shorten the code to the following if you are confident in your control names:
Dim names() As String
names = Split("txtVoornaam,txtAchternaam,txtAfdeling", ",")
Dim ctrlName As Variant
For Each ctrlName In names
With Me.Controls(ctrlName)
.Locked = False
.BorderStyle = 4
.BorderColor = RGB(255, 165, 0)
End With
Next ctrlName
function Lockdown(strControl)
with me(strControl)
.locked = false
.borderstyle = 4
.bordercolor = RGB(255,165,0)
end with
use me or forms!formname depending on where you're calling from
if your controls are the same, obviously put them in a single sub/function that you can call from anywhere. i would not try to lock or change the format of textboxes, instead just enable/disable, and it will handle the format for you:
textbox.enabled = true/false
if you are doing this on multiple forms and really want a single sub/function to control enabling/disabling the textboxes on each form, then there are various ways of doing that as well, solution will depend on your needs, but certainly doable and some have already commented above.
for example, you can use the "tag" property of the textboxes to flag the textboxes on that form that you want to enable/disable. you can then have a single sub/function that would take in the form as reference, and then you can read the "tag" property of all textboxes on that form and if they are the ones you flagged, you would proceed to enable/disable them

Algorithm for finding end of a list (SAP GUI)

I'm writing a script that adds elements to a list in a SAP GUI screen. Now, it seems that when using SAP GUI, nothing "exists" unless it is actually on screen, so the first step involves finding the end of the list.
I accomplished this by scrolling though each element, and checking if it was blank.
Do While Not blank
If session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010/ctxtMAPL-MATNR[2,0]").Text = "" Then blank = True
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position = i
i = i + 1
Loop
However, for very large existing lists, this takes a long time. I'm trying to figure out a way to find the end more quickly. Some truths/limitations I know:
I'm assuming I have no knowledge of the list length.
I cannot command the verticalScrollbar.position too far beyond the end of
the list. For ex. if the list contains 62 elements, .verticalScrollbar.Position = 100 will not work.
In the case of the above example, SAP does NOT throw an error. Nothing happens at all, and then next line of code executes.
All references to elements are with respect to their position on the screen. Ex, if I scroll down 5 positions, the 6th element of the overall list would actually indexed as 1.
On the other hand, verticalScrollbar.Position is absolute
I'm thinking of doing the following (in very psuedocode):
i = 0
do while scrolled = true
scrolled = false
a = GUIlist[0]
verticalScrollbar.Position = i + 1000
b = GUIlist[0]
'check to see the first element shown has changed
if a <> b then
scrolled = true
i = i + 1000
end if
loop
do while scrolled = true
scrolled = false
a = GUIlist[0]
verticalScrollbar.Position = i + 500
b = GUIlist[0]
if a <> b then
scrolled = true
i = i + 500
end if
loop
...and so on until I'm iterating i by one.
Is there a generally accepted better way of doing this kind of 'search'?
Any input is appreciated.
Thanks
My suggestion:
session.findById("wnd[0]").sendVKey 83
myPosition = session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position
do
if session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010/ctxtMAPL-MATNR[2,0]").Text = "" then exit do
myPosition = myPosition + 1
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position = myPosition
loop
msgbox myPosition
Regards,
ScriptMan
Just to go to the end
max_scrollbar = session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Maximum ' Get the maximum scrollbar value
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position = max_scrollbar ' Go to the end

returning a button from a function VB.net

In my program I have to reference different dynamically created buttons and they are stored in an array of records. Depending on what FieldShow is equal to, one of three FlowLayoutPanels are visible, which contain the buttons.
Private Function GetFields(i As Integer) As LoadingTemplate
Dim TempFields As LoadingTemplate
If FieldShow = 0 Then
TempFields.B = Study(i)
ElseIf FieldShow = 1 Then
TempFields.B = Task(i)
ElseIf FieldShow = 2 Then
TempFields.B = Note(i)
End If
Return TempFields
End Function
TempFields.B.RefBtn, if used here, then shows the button that is being returned, But if elsewhere I put
GetFields(x).B.RefBtn
with an appropriate value for X, it sometimes doesn't return things. Is there an easier way of doing this?
Thanks,
Henry