I'm making a server checkpoint in my new version for this app. and I'm encountering this error
when my app tries to open my main form.
An error occurred creating the form. See Exception.InnerException for
details. The error is: ActiveX control
'6bf52a52-394a-11d3-b153-00c04f79faa6' cannot be instantiated because
the current thread is not in a single-threaded apartment.
I'm not sure what this is, because it's something like a usual form except this form is used as Splash screen. When I remove the splash screen the form opens normally with all plugins and modules.
Here's a part of the code I used
Public Class example_form
Public Function servercheck() As Boolean
Dim objUrl As New System.Uri("http://google.com")
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objresp As System.Net.WebResponse
Try
objresp = objWebReq.GetResponse
objresp.Close()
objresp = Nothing
Return True
Catch ex As Exception
objresp = Nothing
objWebReq = Nothing
Return False
End Try
End Function
Private Sub Form4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BackgroundWorker1.RunWorkerAsync()
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If servercheck() = True Then
Form1.Show()
Me.Hide()
BackgroundWorker1.CancelAsync()
Else : PictureBox1.Image = My.Resources._12383u9
MsgBox("some text here", MsgBoxStyle.Critical)
End
End If
End Sub
End Class
Now you have the code. The error is on the background worker when it tries to open the form. (at the end of the code)
Ok i found what is the error. For those who have this error, do not add the form.show() command in the backgroundworker in work mode. the form should be load when backgroundworker will finish. so the correct code is like this
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If servercheck() = True Then
Me.Hide()
BackgroundWorker1.CancelAsync()
Else : PictureBox1.Image = My.Resources._12383u9
MsgBox("Some text here", MsgBoxStyle.Critical)
End
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Form1.Show()
End Sub
Related
I've got a Excel form named UserForm1 with a label with this properties:
This label has the property Visible=False. I want to make visible the label when the user click the CommandButton1, for that I wrote the next code:
Private Sub CommandButton1_Click()
Me.Label6.Visible = True
Dim oficina_garsa, file_source, file_solds As String
Dim invoice_year, invoice_month As Integer
oficina_garsa = TextBox3.Value
file_source = TextBox1.Value
file_solds = TextBox2.Value
invoice_year = CInt(ComboBox1.Value)
invoice_month = ComboBox2.ListIndex
Debug.Print oficina_garsa, file_source, file_solds, invoice_year, invoice_month
Call MainProcess(oficina_garsa, file_source, file_solds, invoice_year, invoice_month)
End Sub
But it doesn't work. So I wrote other event code linked to CommandButton2 to testing purpose like this:
Private Sub CommandButton2_Click()
If Me.Label6.Visible = False Then
Me.Label6.Visible = True
Else
Me.Label6.Visible = False
End If
Application.Wait Now + TimeValue("00:00:03")
End Sub
And the last code works fine while the first one doesn't! But in the first code the next code after Me.Label6.Visible = True runs whole without error message.
I've tried replacing 'Me' by 'UserForm1', but the result is the same. Why assigning the Visible property to True works in the CommandButton2 event but doesn't in the CommandButton1 one?
I know this thread is quite old, but as I came across it with the same problem, I thought I'd post the fix.
After setting the visible argument, add a line
Me.Repaint
This will re-render the form and update the visibility of the element. I think the Repaint command is specific to User Forms, so not sure if this will work in other situations where elements are not rendering accurately.
I'm creating in Excel VBA a form using code. The following code snippet presents a problem in which the form is somehow created with the name already correctly set and then afterwards, in the only place where I set the said variable, it raises an issue saying that there is a form with that name (the variable in case).
Here is my code:
Dim frmName As String
frmName = "frm_" & Replace(CStr(Nome_do_formulario), " ", "")
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With myForm
.Properties("Caption") = Nome_do_formulario
.Properties("Width") = 300
.Properties("Height") = 270
.Properties("Name") = frmName
End With
To be clear, the error is that when it reaches the line:
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
Somehow it already creates a form with name that's set after at the with statement:
With myForm
.Properties("Caption") = Nome_do_formulario
.Properties("Width") = 300
.Properties("Height") = 270
.Properties("Name") = frmName '<- HERE
End With
And then, when it tries to run the with statement it breaks and says that a form with that name already exists.
The whole thing is ran at another module as:
Public Sub Main()
Dim ac As autoCrud
Set ac = New autoCrud
ac.CreateCRUDView
End Sub
The form creation happens inside the ac.CreateCRUDView.
How is it pulling the name variable before it's set and then trying to use it to make another form with the same name?
VBE suffers heavily corruption when it is about UserForms collection in a VBA Project.
Even if you remove explicitly a UserForm from your project, you might get errors creating programatically (and sometimes in the normal way) another with the same name.
Try using this approach:
Dim frmName As String
Dim myForm As VBComponent
frmName = "frm_" & Replace(CStr(Nome_do_formulario), " ", "")
ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm).Name = frmName
Set myForm = ThisWorkbook.VBProject.VBComponents(frmName)
With myForm
.Properties("Caption") = Nome_do_formulario
.Properties("Width") = 300
.Properties("Height") = 270
End With
Remember, if you delete the newly created userform and run this code with the same Nome_do_formulário value, you'll get an error.
Im trying to Contol LEDs using my android device and I'am using Basic4android for the app. I got things working but everytime I press a button to turn an LED on/off.. I got this error saying "android.os.NetworkOnMainThreadException" ... This is my code:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim request As HttpRequest
Dim HttpClient1 As HttpClient
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim Button1 As Button
Dim Button2 As Button
Dim Button3 As Button
Dim Button4 As Button
Dim Button5 As Button
Dim Button6 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
HttpClient1.Initialize("HttpClient1")
End Sub
Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim resultString As String
resultString = Response.GetString("UTF8")
End Sub
Sub HttpClient1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Error connecting: " & Reason &" "& StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
Response.Release
End If
End Sub
Sub Button6_Click
request.InitializeGet("http://192.168.0.8/?BlueOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button5_Click
request.InitializeGet("http://192.168.0.8/?BlueON")
HttpClient1.Execute(request, 1)
End Sub
Sub Button4_Click
request.InitializeGet("http://192.168.0.8/?GreenOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button3_Click
request.InitializeGet("http://192.168.0.8/?GreenON")
HttpClient1.Execute(request, 1)
End Sub
Sub Button2_Click
request.InitializeGet("http://192.168.0.8/?RedOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button1_Click
request.InitializeGet("http://192.168.0.8/?RedON")
HttpClient1.Execute(request, 1)
End Sub
I've researched this issue and it says that I'am doing a network operation on the main thread... I'm new to basic4android.. any tips on how to do the network operations an a separate thread? any help is really appreciated.. thank you :D
best regards,
Caldwell D.
There are two solutions. The simplest would be to disable the strict mode on Android version greater API 9:
Sub DisableStrictMode
Dim jo As JavaObject
jo.InitializeStatic("android.os.Build.VERSION")
If jo.GetField("SDK_INT") > 9 Then
Dim policy As JavaObject
policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
Dim sm As JavaObject
sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
End If
End Sub
Better solution would be to replace the Response.GetString() calls (which are deprecated) with the Asynchronous Response.GetAsynchronously() calls.
I am writing a code in MS Access VBA, which is as follows:
Private Sub Form_Load()
MsgBox "loggedIn = " + CStr(loggedIn)
If (loggedIn = 1) Then
Else
Exit Sub
End If
End Sub
I want to decide whether to load the form or not based on loggedIn variable. If loggedIn variable is 1, form shall be loaded. If the same is not 1, form shall not be loaded.
Issue that I am facing is that, whatever I do in ELSE part of the code above, I am not able to stop the form from loading.
How can I achieve this?
Pls comment if any additional information is required.
Thank you.
If the loggedIn value is available at form open, you could cancel the form open event.
Private Sub Form_Open(Cancel As Integer)
Cancel = Not (loggedIn = 1)
End Sub
If the value of loggedIn is not available until the form's load event, you can close the form.
Private Sub Form_Load()
If loggedIn <> 1 Then
DoCmd.Close acForm, Me.Name
End If
End Sub
Private Sub OccurrenceName_AfterUpdate()
If OccurrenceName.Value = "Other" Then
Dim strTechID As String
Dim strOccurrenceCt As String
Dim strOccurrenceDate As String
strTechID = Me.Parent.tbxTechID.Value
strOccurrenceCt = Forms![frmEmployeeOccurrenceInput]![tbxOccurrence].Value
strOccurrenceDate = Me.OccurrenceDate.Value
Dim strOpenArgs As String
strOpenArgs = strTechID & "|" & strOccurrenceCt & "|" & strOccurrenceDate
DoCmd.OpenForm "frmOtherOccurrence", , , , , , strOpenArgs
Else
Me.OccurrenceAmt = Me.OccurrenceName.Column(1)
Me.Type = Me.OccurrenceName.Column(2)
End If
End Sub
Every time it runs I get "The Open Form action was canceled" with an error code of 2501. The line it gets caught on is the DoCmd.OpenForm call. Debugging give NO additional information.
Here is where the OpenArgs is passed to:
Private Sub Form_Load()
Dim aryOA As Variant
aryOA = Split(Me.OpenArgs, "|")
Me.lblTechID.Caption = aryOA(0)
Me.lblOccurrenceCt.Caption = aryOA(1)
Me.lblOccurrenceDate.Caption = aryOA(2)
End Sub
I don't know if this could be your problem, but you can't pass OpenArgs to an open form, and by open I mean it can't be even in edit mode, should be completely closed.
Otherwise, the form will open (change its status from edit mode to normal) but no OpenArgs will be passed, so OpenArgs will be null and an exception will be thrown.