MS Access - Object from another form isn't recognized - ms-access

Our school project is to make a voting system in MS Access. So far this is what I've done:
Private Sub Command7_Click()
Dim President1 As Integer
Dim President2 As Integer
President1 = 0
President2 = 0
If Frame0.Value = 1 Then
DoCmd.Close
DoCmd.OpenForm "RESULTS"
President1 = President1 + 1
Me.lblpresresults1.Caption = President1
DoEvents
Else
DoCmd.Close
DoCmd.OpenForm "RESULTS"
President2 = President2 + 1
Me.lblpresresults2.Caption = President2
DoEvents
End If
End Sub
There are two forms, voting1 and RESULTS
There are two voting options in form voting1. When clicking a button beneath the two options, it should add +1 to a counter. And the resulting number should appear in a textbox in the form RESULTS.
But whenever i run it, it says "method or data member not found". Can someone help point out where the code went wrong? Is there something missing? Thanks in advance.
EDIT: The above code is the code in the aforementioned button

debugging.
try commenting out all the suspect lines using ' at the start of the line.
un-comment one at a time to find out which one is breaking your system.

You are closing the form before opening the next, thus pulling the feet under your code. Try:
Private Sub Command7_Click()
Dim President1 As Integer
Dim President2 As Integer
DoCmd.OpenForm "RESULTS"
If Frame0.Value = 1 Then
President1 = President1 + 1
Forms!RESULTS!lblpresresults1.Caption = President1
Else
President2 = President2 + 1
Forms!RESULTS!lblpresresults2.Caption = President2
End If
DoCmd.Close
End Sub
However, that will alway set the caption to "1", not adding anything. So perhaps:
Private Sub Command7_Click()
Dim President As Label
Dim Votes As Long
DoCmd.OpenForm "RESULTS"
If Frame0.Value = 1 Then
Set President = Forms!RESULTS!lblpresresults1
Else
Set President = Forms!RESULTS!lblpresresults2
End If
Votes = Val(President.Caption) + 1
President.Caption = CStr(Votes)
DoCmd.Close
End Sub
That won't save the votes anywhere, but that - I guess - is the next task.

Related

Access VBA: this application has requested the Runtime

Running a VBA macro, sometimes it works well. But in some cases, I get the following error.
I have debugged the code but I don't get any error. The code is the following.
Public Sub Adjust_ComboBox()
On Error GoTo Adjust_ComboBox_Err
Dim mes As Form
Set mes = [Form_Treatment Details]
Dim count As Long
On Error Resume Next
For Each ctl In mes.Detail.Controls
If TypeName(ctl) = "ComboBox" Then
Dim comboitems() As Variant
count = 1
comboitems = ctl.Value
count = UBound(comboitems) + 1
ctl.Height = (300 * count)
Erase comboitems
End If
Next
Adjust_ComboBox_Exit:
Exit Sub
Adjust_ComboBox_Err:
MsgBox Error$
Resume Adjust_ComboBox_Exit
End Sub
How can it be possible to get this error?
This whole block of code looks odd to me.
Dim comboitems() As Variant
count = 1
comboitems = ctl.Value
count = UBound(comboitems) + 1
ctl.Height = (300 * count)
Erase comboitems
My questions are:
Why are you DIMing and ERASEing the combotems in a loop?
Why are you setting your array to the control's value?
Are you trying to set the height based on the number of items?
Why are you setting count=1 when it just gets overwritten two lines
later?
If I am guessing right on the answers to the questions I think you'd eliminate a few possible crashes by slimming down your loop code and using ListCount:
count = ctl.ListCount
if count > 0 then
ctl.Height = (300 * count)
else
ctl.Height = 300
end if

How do I compare the result of a function to values in a list box to determine which form to open on start up?

I'm trying to employ a basic level of security to my Access db.
I have a list of valid users and want to compare them to user name from advapi32.dll.
The code below is where I've got to with my AutoExec macro:
Function UserCompare()
Dim c As Control
Dim i As Integer
Dim y As Boolean
DoCmd.OpenForm ("Users_Form")
Set c = Forms!Users_Form!lst_Users
For i = 0 To c.lst_Users.ListCount - 1
If c.lst_Users.Column(0, i) = ThisUserName() Then
y = True
End If
Next i
If y = True Then
DoCmd.OpenForm ("Login Screen - Authorised")
Else
DoCmd.OpenForm ("Login Screen - Unauthorised")
End If
DoCmd.Close acForm, "Users_Form", acSaveNo
End Function
This is resulting in a run-time error 438 (Object doesn't support this property or method).
Can someone point me in the right direction?
Many thanks,
Chas
Try this:
Set c = Forms!Users_Form!lst_Users
For i = 0 To c.ListCount - 1
If c.Column(0, i) = ThisUserName() Then
y = True
End If
Next i

How to create controls at run time Access VB?

How can you create controls at run time with VB code in Microsoft Access? after some digging I found that this is possible with the CreateControl function. The problem is every random forum I find online has some code similar to this:
Private Sub Button_Click()
Call AddLabel
End Sub
Function AddLabel()
Set ctl = CreateControl("MyForm", acLabel, acDetail, "", "", 0, 0, 100, 100)
With ctl
.name = "myTextBox"
.Caption = "Hello World!"
.Height = 50
.Width = 100
.FontSize = 11
.Visible = True
End With
End Function
but this code appears to not create a visible label.
In my case I'm just trying to learn how to get this to work. So I created a blank form with a button that when clicked will create a label that says "Hello world!". What I'm expecting to happen is a textbox will display in the top left of the form when the button is clicked. I'm curious if anyone could help show me a simple example of some code that actually works.
Before anyone says I can create a label and hide it then change its visibilty property, I know. But I'd like to know how to create controls dynamically and getting this simple example to actually work will greatly help my understanding.
The documentation you need is here (these are specifically for Access VBA):
Application.CreateControl Method (Office 2007)
Application.CreateControl Method (Office 2003)
According to the documentatin, there are some big limitations to this feature:
Limited to 754 controls over the lifetime of the form (this is not reset by deleting them, so you are likely to run into this limit quickly)
Must be done in Design view (so it can't be done in mde/accde)
It is questionable how it will perform in a multi-user environment.
Because of these limitations, it is inadvisable, unless you are using to design forms initially.
Duplicate Question: How do you dynamically create controls on a MS Access form?
In response to the OP's suggestion, here is my test code which was able to add 40 controls and repeat the process 50 times without exceeding the 754 limit (I reused 40 names in my test).
Caveat 1 This is inadvisable because it can only be done in design view which will not work in an mde/accde.
Caveat 2: It is questionable how it will perform in a multi-user environment.
This code is from a form with two buttons. It opens a second form named "Form2"
Option Compare Database
Option Explicit
Private Const FORM_NAME As String = "Form2"
Private m_nCounter As Long
Private Sub cmdCreate_Click()
runDynamicForm
End Sub
Private Sub cmdRepeat_Click()
Dim n As Long
m_nCounter = 0
For n = 0 To 50
runDynamicForm
DoEvents
DoCmd.Close acForm, FORM_NAME, acSaveNo
DoEvents
Next 'n
MsgBox m_nCounter
End Sub
Private Sub runDynamicForm()
Const DYNAMIC_TAG As String = "dynamic"
Dim n As Long
Dim frm As Form
Dim ctl As Access.Control
On Error GoTo EH
Application.Echo False
DoCmd.OpenForm FORM_NAME, acDesign
Set frm = Application.Forms(FORM_NAME)
For n = frm.Controls.Count - 1 To 0 Step -1
Set ctl = frm.Controls(n)
If ctl.Tag = DYNAMIC_TAG Then
Application.DeleteControl FORM_NAME, ctl.Name
End If
Next 'n
For n = 1 To 20
With Application.CreateControl(FORM_NAME, acLabel, acDetail, , , 400, n * 300, 1500, 300)
.Name = "lbl" & n
.Caption = "Question " & n
.Visible = True
.Tag = DYNAMIC_TAG
End With
With Application.CreateControl(FORM_NAME, acTextBox, acDetail, , , 2000, n * 300, 3000, 300)
.Name = "txt" & n
.Visible = True
.TabIndex = n - 1
.Tag = DYNAMIC_TAG
End With
m_nCounter = m_nCounter + 2
Next 'n
DoCmd.Close acForm, FORM_NAME, acSaveYes
DoCmd.OpenForm FORM_NAME, acNormal
GoTo FINISH
EH:
With Err
MsgBox "Error:" & vbTab & .Number & vbCrLf _
& "Source" & vbTab & .Source & vbCrLf _
& .Description
End With
FINISH:
Application.Echo True
End Sub
I took that upove code and simplified it as it was long winded, and turned it into a a sample code for my own future use. Hope it helps someone in the future.
Public Sub runDynamicCreateControls()
Dim FormName As String
Dim NumControls As Integer
Dim n As Long
Dim ctl As Access.Control
Dim ctlname As String
On Error GoTo EH
Application.Echo False
FormName = "createcontrolF" 'Input Name of Form
NumControls = 20 'input number of controls
ctlname = "txt" 'input control name
DoCmd.OpenForm FormName, acDesign
For n = 1 To NumControls
With Application.CreateControl(FormName, acTextBox, acDetail, , , 1000,1000, 1100, 600)
.Name = ctlname & "_" & n
.Visible = True
.Tag = n
End With
Next 'n
DoCmd.Close acForm, FormName, acSaveYes
DoCmd.OpenForm FormName, acNormal
GoTo FINISH
EH:
With Err
MsgBox "Error:" & vbTab & .Number & vbCrLf _
& "Source" & vbTab & .Source & vbCrLf _
& .Description
End With
FINISH:
Application.Echo True
End Sub
Whenever I attempt to run your code I get the runtime error of:
Run-time error '6062':
You must be in Design or Layout View to create or delete controls.
Based off of that information it seems like dynamically creating controls in runtime isn't going to be possible.
You might be only missing DoCmd.Restore, here is an example on how to dynamically create form, data bind it, and create controls, all in runtime.
Sub NewControls()
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
' Create new form with Orders table as its record source.
Set frm = CreateForm
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100
' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
End Sub

Access VBA - All checkboxes on form have been checked

I am relatively new to Access VBA and have a form that has around 30 checkboxes on it. When saving the form I want to ensure that all checkboxes have been ticked (set to true). The tickboxes have all got names SC1, SC2....SCN Is there a way to loop through each control and see if it has been set to true?
This is what I have tried but it doesnt seem to read the tickbox -
Private Sub Validate_Data(rstTop)
Dim n As Integer
Dim count As Integer
count = 0
For n = 1 To rstTop
If Form.Controls("SC" & n).Value = False Then
count = count + 1
End If
Next
If count <> 0 Then
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _
"More information Required"
Else
End If
End Sub
Give this a try, it worked for me.
Option Compare Database
Option Explicit
Private Function Validate_Data() As Boolean
Dim ctl As Control
Validate_Data = True 'initialize
For Each ctl In Me.Form.Controls
If ctl.ControlType = acCheckBox Then
If (ctl.Name Like "SC*") And (ctl.Value = False) Then
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _
"More information Required"
Validate_Data = False 'something isnt checked
Exit Function
End If
End If
Next ctl
End Function
Private Sub cmdGo_Click()
Validate_Data
End Sub

Automatically Close Form After Certain Idle Time

I am having a little bit problem on my access form.
I have 2 forms:
menu-form
input-form
After input-form has been idle for one minute, I want to close it and go back to the menu-form.
This is my code that isn't working.
Public ExpireTime As Date 'expiration time-date value
Sub ResetExpiration()
If ExpireTime <> 0 And ExpireTime > Now Then
Application.OnTime ExpireTime, "FormExpire", schedule:=False
End If
ExpireTime = Now + 1 / 1440#
Application.OnTime ExpireTime, "FormExpire", schedule:=True
End Sub
I also created a macro with this in it.
Sub FormExpire()
Unload input-form
End Sub
You need to set the form.Timer to 60000 (that is 1 minute) then use on OnTimer event to check if some properties have changed. Below I quickly wrote something to give you an idea, but it is not complete nor tested.
Option Compare Database
Option Explicit
Dim isIdle As Boolean
Private Sub Form_LostFocus()
'you can use this event also
End Sub
Private Sub Form_Timer()
Dim ctlName As String, wasDirty As Boolean
If ctlName = vbNullString Then ctlName = Me.ActiveControl.Name
If Me.ActiveControl <> ctlName Then isIdle = False
If wasDirty <> Me.Dirty Then isIdle = False
'more checks....
If isIdle Then DoCmd.Close acForm, Me.Name
End Sub
You've got a couple of potential issues here.
First and foremost, that's not how you close a form.
This:
Sub FormExpire()
Unload input-form
End Sub
Should look something more like this:
Sub FormExpire()
DoCmd.Close acform, Me.Name
DoCmd.OpenForm "menu-form"
End Sub
Your second issue is that I don't believe the Application.Ontime method is available from Access. MSDN says it's an Excel function, and I can't find it in the Access Documentation. I'd personally be interested in seeing you make your method work with the Access form Timer Event.
I'm not sure it matters, but I think this is what you were trying to do.
Public ExpireTime As Date 'expiration time-date value
Private Sub InputForm_Timer()
If ExpireTime <> 0 And ExpireTime > Now Then
Call FormExpired
Else
' reset timer
ExpireTime = Now + 1 / 1440#
End If
End Sub
I'm not sure this method would work without also including #iDevelop's answer. I just wanted to help clarify where you were going wrong.
I realize that this thread is aged but I recently experienced a need to close forms from a timer event and came across the answers here. My situation was slightly different as I had several forms to close so here is my solution.
Private Sub Form_Timer()
Dim daysRemaining As Integer
Dim endDate As Date
endDate = "4/15/2021"
daysRemaining = DateDiff("d", Now, endDate)
MsgBox ("This is an evaluation version of the application. You have " & Str(daysRemaining) & " days of use remaining")
If daysRemaining < 1 Then
Close_All
End If
End Sub
Private Sub Close_All()
For Each myForm In CurrentProject.AllForms
strFormName = myForm.name
DoCmd.Close acForm, strFormName
Next
End Sub