form_load subroutine in MS Access VBA - forms

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

Related

VBA Access form: Submit Button to create new record but keep some values in the form

I have a form to enter all necessary data. With the wizard I created a button that saves the data as a new record, but this button clears the form.
I want this button to do exactly that, but keep some of the entered values in the form, because those values will stay the same for a certain amount of records.
Currently my button runs this code generated by the button creation wizard:
Private Sub submit_btn_Click()
On Error GoTo submit_btn_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
submit_btn_Click_Exit:
Exit Sub
submit_btn_Click_Err:
MsgBox Error$
Resume submit_btn_Click_Exit
End Sub
When the button is clicked I want to clear all values in the form, except for the date and a group field. Can I easily do this in this code or is there a way to do this via the default value property of these fields?
Try the following:
Go to the properties of your form and put the following code in the BeforeUpdate event:
Private Sub Form_BeforeUpdate(Cancel As Integer)
entry_date.Tag = CLng(entry_date.Value)
acc_value.Tag = acc_value.Value
End Sub
In the Current event of the form, put the following code:
Private Sub Form_Current()
If Me.NewRecord Then
entry_date = CDate(entry_date.Tag)
acc_value = acc_value.Tag
End If
End Sub
That is all you need to do. If you have any questions please let me know

Word Form - using VBA to enable textbox on checkbox click

I have the following VBA code to enable a textbox everytime a checkbox is clicked.
Private Sub CheckName1_Click()
If CheckName1.Value = True Then
TextName1.Enabled = False
TextName1.SpecialEffect = fmSpecialEffectFlat
Else
TextName1.Enabled = True
TextName1.SpecialEffect = fmSpecialEffectSunken
End If
End Sub
The problem is my document will probably have 30 of these by the time I am done (CheckName1, CheckName2, CheckRent1, CheckRent2, etc). I am already having trouble with Word lagging, so I'd like to create a subroutine that will call this sub instead of copy/pasting it again and again.
I haven't spent a lot of time with functions and the like- and even less with VBA. I just know the basics and they always make my head spin. So, this is what I have.
I just don't know how to pass the textbox parameter, or if I even have the checkbox parameter right.
And if this is close, do I still create a call for each checkbox?
Private Sub CheckName1_Click()
Call NA
End Sub
Private Sub NA(checkbox, textbox)
If checkbox.value = True Then
textbox.Enabled = False
textbox.SpecialEffect = fmSpecialEffectFlat
Else
textbox.Enabled = True
textbox.SpecialEffect = fmSpecialEffectSunken
End Sub
Thanks in advance for any help!
Loop on all controls
Based on this example: http://www.ozgrid.com/VBA/control-loop.htm
Dim cCont As Control
For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" Then
'DO STUFF HERE
End If
Next cCont

basic4android android.os.networkonmainthreadexception 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.

Access VBA visible control form from another form

I have a table and form setup to control another form in my database.
I'm wanting to make a code that will take the title from my field and add it to my code as a variable to change the visibility options of my other form.
my form is set with all the of the names to all objects on the form I want to control.
LSE_FORM_ADMIN = The table with all the LSE_FORM_ALL names in it.
Table is setup with 3 columns key, names and a checkbox which I put into a form to make a continuous list.
here is my code on the form, but I keep getting and runtime 424: object required error:
Private Sub Form_Current()
Dim VARSET As Object
Dim VAR As String
VARSET = DLookup("TITLE", Table!LSE_FORM_ADMIN, "") 'keep getting error here
VAR = VARSET
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!VAR.Visible = True
Else
Form_LSE_FORM_ALL!VAR.Visible = False
End If
End Sub
can someone help me fix this code so that it will grab the title field data and make it a variable to add to the rest of the code?
It's difficult to see exactly what you are trying to achieve, but your problems stem from using the variant variable type when you should be using an explicit Form or Control type. Using your last example.
RSTT.Visible = True 'getting Run-time error '424': object required
This is because you have declared RSTT as a variant. The line
RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
results in the variable RSTT containing a string, which does not have a property ".Visible"
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
These lines are redundant as you have the values that you need available on the form fields which are already bound to the table LSE_FORM_ADMIN.
As far as I understand, you have a continuous form (ADMIN?) bound to the table LSE_FORM_Admin. As you step through the records on this form, you want code to be fired which takes the value of the TITLE field/control and use it to set a control with the same name, on a separate form, Form_LSE_FORM_ALL, to be (in)visible, dependent on the value of the checkbox control name CB on the ADMIN form?
If you want the ADMIN form to make the changes "live" to the ALL form, you should consider using an event of the CB checkbox control. Using the current event of the form means that the changes you make will not be reflected in the ALL form until you step out of the record you have just edited, then back in, to fire the form's Current event on that record.
Example using AfterUpdate event of CB checkbox
Private Sub CB_AfterUpdate()
Dim strRST As String
Dim frmTarget as Form
Dim ctlRSTT As Control
Set strRST = Me!TITLE
Set frmTarget = Forms("Form_LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
ctlRSTT.Visible = Me!CB 'getting Run-time error '424': object required
End Sub
really not sure how to do the syntax when doing a recordset to a table from the form, need some help with that.
here is my code and attempt at the record set:
Private Sub Form_Current()
Dim DB As Database
Dim RS As Recordset
Dim RST As String
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Set RST = RS 'GETTING OBJECT REQUIRED ERROR ON "RST ="
Do Until RS.EOF
RST = Me!TITLE
RS.MoveNext
Loop
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!RS.Visible = True
Else
Form_LSE_FORM_ALL!RS.Visible = False
End If
End Sub
I think I know what you are trying to do, but your descriptions / references are not matching up. Please look at the following comments and clarify:
1. You say "...make a code that will take the title from my field and ..." but your code is taking "Me.Title", "ME" is a reference to the Form - not a field.
2. Your code is in the "Form_Current" event, which means it will fire for every record you process. That will work, but I think you want to do this code only once to be more efficient.
3. You have no provision for processing more than one field. I think you need to loop through all fields in your table, setting visible to true or false.
The following is my suggestion, but I will update once you clarify the issues.
Option Compare Database
Option Explicit
Dim DB As DAO.Database
Dim RS As DAO.Recordset
'Dim RST As Variant
'Dim RSTT As Variant
Public Sub FORM_CURRENT()
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Do While Not RS.EOF ' Loop thru all field names for the form
If RS!HideYN = True Then ' Desire to hide the field?
Me(RS!ctlname).Visible = False ' Yes, hide the field.
Else
Me(RS!ctlname).Visible = True ' No, show the field
End If
RS.MoveNext ' Get next field name
Loop
RS.Close
Set RS = Nothing
Set DB = Nothing
'Set RST = Me!Title
'RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
'If Me!CB = "-1" Then
' RSTT.Visible = True 'getting Run-time error '424': object required
'Else
' RSTT.Visible = False
'End If
End Sub
Final code, thanks to Cheesenbranston.
Private Sub Form_AfterUpdate()
Dim strRST As String
Dim frmTarget As Form
Dim ctlRSTT As Control
strRST = Me!TITLE
Set frmTarget = Forms("LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
If Me!CB = "-1" Then
ctlRSTT.Visible = True
Else
ctlRSTT.Visible = False
End If
End Sub
#Cheesenbranston: Your original code was more like a toggle of on and off so if my object was not visible then my trigger checkbox would make it visible when checked, more of a quality of life for my own needs, none the less worked. Also strRST doesn't need SET since its just a String. Thanks again =D very happy day!

How to open another Form from current Form?

Our first form is the LOG IN form..how can I open to the next form after logging in?
In your log-in form, I assume you perform your validation inside of the Click event method for a button control. So you would have something like:
Private Sub btnLogin_Click()
If ValidatePassword(txtPassword.Text) Then
' The password is correct, so show the main form and close the login form
MainForm.Show
Unload Me
Else
' The password is incorrect, so leave this form on screen
MsgBox "Invalid password entered!", vbError
txtPassword.SetFocus
End If
End Sub
The two interesting features of this code are:
The Show method, which you call on the form object that you want to show.
In this case, it will probably be your main form—replace MainForm with whatever it is called.
The Unload statement, which closes and destroys the specified form.
In this case, Me refers to the login form since you're finished with it.
You will need call Show on the form which needs to be displayed post login form. You can read more about Understanding Forms and form events
My approach is to avoid trying to open a logon Form as the first Form.
Instead let the main Form be first, and in its Load event Show your logon Form as a modal dialog. This can be done revealing the main Form first by doing a Show on it. Example based on the standard template "Log in Dialog" Form with some code changes:
frmMain.frm
Option Explicit
Private Sub Form_Load()
Dim Control As Control
Show
frmLogin.Show vbModal, Me
With frmLogin
txtSuccess.Text = CStr(.LoginSucceeded)
If .LoginSucceeded Then
'Proceed normally, perhaps after capturing
'the User Name, etc.
txtUserName.Text = .User
txtPassword.Text = .Password
Else
'Do "Unload Me" or disable all controls
'as shown here, etc.
For Each Control In Controls
On Error Resume Next
Control.Enabled = False
On Error GoTo 0
Next
End If
End With
Unload frmLogin
End Sub
frmLogin.frm
Option Explicit
Public LoginSucceeded As Boolean
Public User As String
Public Password As String
Private Sub cmdCancel_Click()
LoginSucceeded = False
Hide
End Sub
Private Sub cmdOK_Click()
'Check for correct password, hard-coded here.
If txtPassword.Text = "password" Then
LoginSucceeded = True
User = txtUserName.Text
Password = txtPassword.Text
Hide
Else
MsgBox "Invalid Password, try again!", , "Login"
With txtPassword
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
End Sub