VB - Calling Sub doesn't work from all classes - vb.net

I have a sub (Named "StartStop") that is called from a couple different buttons as well as from another form. All of the forms are their default instance and the sub is called the same way each time:
The Buttons on the "Main" Form:
Call StartStop(Start.Text, "Server")
Call StartStop(Weekend.Text, "Server")
From the problem Form:
Select Case MessageParse(2)
Case "Start"
Call Main.StartStop("Start", MessageParse(1))
Case "Stop"
Call Main.StartStop("Pause", MessageParse(1))
Case "Weekend"
Call Main.StartStop("Weekend", MessageParse(1))
Case "Weekday"
Call Main.StartStop("Weekday", MessageParse(1))
My issue is that from my problem form, (named "CommServer"), it calls the sub and runs through the code but does not execute any of it. I can step through it and watch it run line by line without any apparent change.
If I call it the same way from a third unrelated form, for the sake of testing, everything works the same way it does from the buttons which is leading me to believe it's something with my CommServer form. Here's a snippet from the called Sub:
Public Sub StartStop(ByVal Action As String, ByVal User As String)
Select Case Action
Case "Start"
Countdown.Interval = 500
TargetDT = DateTime.Now.Add(TimeLimit)
Countdown.Start()
GameUpdateTimer.Interval = GameSpeed * 1000
GameUpdateTimer.Start()
Start.Text = "Pause"
Weekend.Enabled = True
GameOn = True
CommServer.xUpdate("StartStop" & "`" & "Start", True)
CommServer.xUpdate("ChatMessage" & "`" & User & " Started The Game", True)
I can step through the sub, and even watch it loop back to the CommServer form and run through the xUpdate Sub over there, except nothing happens. However if I dropped the line MsgBox("Test") in that code it would produce a message box but ignore everything else.
So my question is, what should I be looking for in my CommServer form that would pass by the code without executing it? I've scoured my code and can't find any evidence that these forms are not the default instance.
Thanks in Advance, I hope it's easy!

Related

Context-menu and keyboard shortcut return different results on the same method

I have a vba sub ("sub") in excel 2013, which opens another workbook, reads some data out of it, return this data and close the newopened workbook. It is possible to run this sub via keyboard shortcut and a entry in the context menu.
This call (the "UTILS.sub" this is) works perfectly fine:
' Add the sub-call to a new context menu entry
Call UTILS.addContextMenuEntry("Caption", 2556, "UTILS.sub")
But this call doesn't:
' Add the sub-call to a new keyboard shortcut
App.OnKey "+^{M}", "UTILS.sub"
If i call sub with the keyboard shortcut it breaks without an error. I've managed to work out the specific code line, at which it breakes via debugging:
'[...]
Application.ScreenUpdating = False
' Open the external Workbook
Set wbHandle = Workbooks.Open("wb.xls", ReadOnly:=True)
MsgBox "Debug"
'[...]
wb.xls opens (and displays), but the MsgBox "Debug" does not. Nothing after the "Open"-line does run and no breakpoint after this line will be hit. Another strange thing: If i debug a call of sub with a breakpoint before that line, it all works perfectly.
How to get the sub to run correctly, not regarding wether it was called by a context menu entry or keyboard shortcut?
I'm not exactly sure what the cause of this behavior is, but I suspect that macros called via keyboard shortcuts or context menus are actually passed through Application.Run or something else that can reset the execution state internally.
The simple work-around seems to be to pass execution through a "wrapper" sub:
Public Sub bar()
foo
End Sub
Public Sub foo()
Dim bar As Workbook
Set bar = Workbooks.Open("C:\Book1.xls", ReadOnly:=True)
MsgBox "foo"
End Sub
Launching foo by a keyboard shortcut will not display the message box, but calling bar will.
I've found the answer to my problem by myself: stackoverflow.com/questions/17409524/
Apparently, excel can't handle the "Workbooks.Open()"-method if the sub is called through a keyboard shortcut, which includes the [SHIFT]-key. Solution: Use "Workbooks.Add()" instead.

How to call a function in Visual Basic

Im fairly new at visual basic and im having trouble using function. ive tried many ways but failed, so ive deleted the funtion to start again. i want this function to run once someone has clicked a button, however the button is on a different form.
When button(btnAdd) is clicked on form 2 i want this to run on form one...
Using writer As System.IO.StreamWriter = New System.IO.StreamWriter(filepath", True)
Dim recipient As String = tbRecipient.Text
If (tbRecipient.Lines.Count > 1) Then
recipient = ""
For Each line As String In tbRecipient.Lines
recipient = recipient & " " & line
Next
recipient = recipient.Trim()
End If
writer.WriteLine(recipient)
End Using
Im not sure if this is the right code to achieve what i want it to do. What the code should do is when the user clicks the button add, it reads the checked options in a checklistbox and adds them to a file. that file is the outputted to a different textbox, which is on a different form. I have the funtion working correctly for the adding of the checkboxlist but need to then display it in a text box on another form. if anyone can help or point me in the right direction that would be great.
Double click on the button(btnAdd) in design window then write:
myFunction()
then in the second form code window:
public function myFunction()
'your code
end function
just call the function name where you want to execute it, or using the reserved keyword Call as Below:
First Way:
'Call by taping the function name
myFunctionName(Argumets)
Second Way:
'Use the Call Keyword:
Call myFunctionName(Argumets)

SetFocus inside a GotFocus procedure initiated by another SetFocus

Objective: Redirect focus from one command button to another using the first's GotFocus procedure.
Context: I have a form-independent procedure in a generic module that, on most forms, sets focus to the NewRecord button after saving the previous record. But on one form, I would like to redirect (based on certain conditions) focus back to the SignRecord button so the user can "sign" a second part of the same record (I may need this for other uses in the future). The target control is enabled and visible and can otherwise be focused and the original control can be focused when the redirect doesn't occur. Reference [2] below implies that this should be possible, though I'm not changing visibility of my controls.
Issue: When the conditions are met to redirect focus in the GotFocus procedure, it redirects as desired but the original (test) SetFocus call throws a "Run-time error '2110', Can't move focus to the control CommandNew".
What I've tried:
Exit Sub after my downstream SetFocus calls.
Call CommandSign.SetFocus in the hopes that it would make it happen outside the previous SetFocus process.
In a module,
Public Sub test()
Forms("TargetForm").CommandNew.SetFocus 'This gets the error '2110'
End Sub
In the 'TargetForm',
Private Sub CommandNew_GotFocus()
If IsNull(textDateTime) Then Exit Sub 'Works as expected
'I can see these two parts work. The framSign value changes
'and CommandSign gets focus
If checPPC And IsNull(textSigID_PPC) And framSign = 2 Then
framSign = 1
CommandSign.SetFocus
ElseIf checDAS And IsNull(textSigID_DAS) And framSign = 1 Then
framSign = 2
CommandSign.SetFocus
End If
End Sub
References:
[1]: SelectNextControl() a bad idea in a GotFocus event?
[2]: http://www.access-programmers.co.uk/forums/showthread.php?t=100071
I think your problem is that the call to Forms("TargetForm").CommandNew.SetFocus doesn't quite seem to, in fact, finish setting the focus to CommandNew until after Private Sub CommandNew_GotFocus() has finished executing. Because you've called another SetFocus before the first SetFocus could finish, there is a conflict that Access seems to be unable to cope with.
Whether or not that is the case, one thing is clear: the way you have your execution plan set up right now is unfortunately not going to work. You might try adding either a global variable or a public variable to each form that determines whether or not you should set your focus to CommandSign after you set the focus to CommandNew.
Ex. TargetForm:
Public boolSetCommandSignFocusInstead As Boolean
Private Sub CommandNew_GotFocus()
If IsNull(textDateTime) Then Exit Sub 'Works as expected
'I can see these two parts work. The framSign value changes
'and CommandSign gets focus
If checPPC And IsNull(textSigID_PPC) And framSign = 2 Then
framSign = 1
boolSetCommandSignFocusInstead = True
ElseIf checDAS And IsNull(textSigID_DAS) And framSign = 1 Then
framSign = 2
boolSetCommandSignFocusInstead = True
Else
boolSetCommandSignFocusInstead = False
End If
End Sub
Module:
Public Sub test()
Forms("TargetForm").CommandNew.SetFocus
If Forms("TargetForm").boolSetCommandSignFocusInstead Then
Forms("TargetForm").CommandSign.SetFocus
End If
End Sub

VBA - The Form Class has no the show method

I want create one form from another. But the Form class has no the Show method, which described at http://msdn.microsoft.com/en-us/library/office/gg251540.aspx
It's code in Form_Main:
Private Sub btnTemp_Click()
Dim frmOpt As Form_Option
Set frmOpt = New Form_Option
frmOpt.Show vbModal
End Sub
But I received the "Compile error: Method or data member not found".
Where I made mistake?
Thanks
(VBA version 6.5; Access 2007)
=====
Sorry for my previous comment: right now I see that comment isn't obvious.
I don't have subForm on my mainForm.
I have two simple form: Form_Main and Form_Option. And I want to be the next logic:
Form_Main has button "btnOption"
Click on "btnOption". The Form_Option is opening
I change options on Form_Option
And click the btnSave button on Form_Option, and the next idea is executing:
Form_Main.TimerInterval = CLng(Form_Option.edtTimerInterval.Value)
At the moment I made it simple. And that is enough for me.
I write so:
Private Sub btnOptions_Click()
' After changing options, refresh timer interval of main form
DoCmd.OpenForm "Options", , , , , acDialog
Me.TimerInterval = 1000 * CLng(MOptions.loadOption("fPeriodVerifyNoticeInterval"))
End Sub
Where fPeriodVerifyNoticeInterval is parameter that stored in the options table.
And the Options Form changes the "fPeriodVerifyNoticeInterval" parameters at saving.
My problem is solved, Thanks
The "mistake" is that Show isn't a valid Method for Access Forms. The link you provided is for UserForms which are forms made in VBA.
If you want to create a new form that way what you want is something like this:
frmOpt.Modal = true
frmOpt.Visible = true
Though what I would recommend is doing this instead:
DoCmd.OpenForm "Option", , , , , acDialog which will open the Option form as a dialog.
Caution: If you create your form using New even though you set it as modal it will not halt the progress of VBA code. This means that your variable will go out of scope as soon as the code finishes. If you want your form to remain open, you will need to set it as static within the sub or declare it outside the sub like this:
static frmOpt As Form_Option
or outside the sub private frmOpt = Form_Option or public frmOpt = Form_Option

I can't show a form that isn't top level with form.ShowDialog()

My startup form is a modal security form which works fine. But, if the user "logs out", the security form must be displayed again as a modal dialog. This last step is where everything goes wrong. It shows the form, in front of my other forms, but it's not modal...
First, I call a method that's written in a module, because I have to be able to call this method from every form I want.
Public Sub CallWaiterKey()
Dim oForm As frmWaiterKey = New frmWaiterKey()
Try
If mWaiterKey.Length > 0 And mWaiterKeyType.Length > 0 Then
If Convert.ToInt32(mWaiterKey) > 0 And Convert.ToInt32(mWaiterKeyType) = 2 Then
oForm.TypeOfKey = 2
ElseIf Convert.ToInt32(mWaiterKey) > 0 And Convert.ToInt32(mWaiterKeyType) = 1 Then
oForm.TypeOfKey = 1
End If
'here it goes wrong
oForm.ShowDialog()
End If
Catch ex As Exception
MsgBox(ex)
End Try
End Sub
When I call oForm.ShowDialog() (that's the frmWaiterKey), it comes up but isn't modal.
I can still click the buttons that are placed on frmMenu, the form from which I called CallWaiterKey().
Am I doing something wrong here?
Or should I make the call in an other way?
(My VB sucks so ignore syntax errors)
To achieve what you are asking, specify the hosting form.
Public Sub CallWaiterKey(ownerForm as Form)
Dim oForm As frmWaiterKey = New frmWaiterKey()
' ....
'here it goes wrong
oForm.ShowDialog(ownerForm)
' ....
End Sub
I don't use ShowDialog; but I believe that you need to specify the window owner to enforce the modality. If I'm wrong here, others will correct me.
oForm.ShowDialog(me)
** HOLD ON ** I will alter this in a second, I just recalled that you're calling from a module, me doesn't evaluate in a basic module.
Here is a MSDN reference