Microsoft Access VBA If statement - vba

I have a dropdown menu that has three options "Open, Assigned, Closed" I have three rows in a table in my database, one for openTime, assignedTime, and closedTime.
My dropdown menu is connected to a row in my table called Status which is swapped between the three options "Open, Assigned, Closed". When the ticket is created in my website, it creates an open time so I don't have to worry about that, but when the user selects assigned and closed I would like it to on change send data to the correct cell.
So if the drop down is swapped from open to assigned it already changes the status cell to "Assigned" in my database but I would also like it to change the assignedTime cell's data to the exact time and date. Same for closed.
I think this is simple I'm just a bit confused on the process. Im using this site for info on IF statements
I have my code that creates and adds my date
me.MyDateTimeColumn = now()
I have a few questions
1) In my IF statement how do I say the value of the selection for example if the user selects "Assigned' in the drop down menu how do I say thats the info I want my IF statement looking at?
On the site linked above this is there example for an if statement:
Sub AlertUser(value as Long)
If value = 0 Then
AlertLabel.ForeColor = "Red"
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
End If
End Sub
On this do I need to include the Sub AlertUser(value as Long)(using my own variables) If so what would my sub be?
and does the If value = 0 Then refer to the value of the selected comboBox?
2) Can I insert an else if as I would in other languages, do I need an end else if?
I am using a microsoft access form, and using the VBA code section for this. MY database is in a sql server connected to the form.

Related

Disallowing textbox editing if a ComboBox in the same Form is not filled (for each row individually) [duplicate]

I am working on an Access 2010 form which where the user can select a record in the form header via a combobox and then build up elements related to the selected record in the detail section of the form. The default view of the form is set to continuous forms.
One of the controls in the detail section of the form is a combobox control. What I want to do is set the enabled property of a textbox on the same row of the form to false based upon a selection from the combobox. The code I am running is:
If Me.cboElementType = "Contract Shrink" Then
Me.txtElementID = ""
Me.txtElementID.Enabled = False
EndIf
This works, but it sets all instances of the textbox (txtElementID) to enabled = false. What I want to have happen is for the txtElementID to have a different enabled setting for each row in the detail section based upon the selection of the combobox cboElementType. So, if cboElementType = "Contract Shrink" on row 1 of the scrolling detail section, the txtElementID.Enabled would be set to false for that row. If cboElementType = "Cost Group" on row 2 of the scrolling detail section, then I'd like txtElementID.Enabled to be False on row 1 of the detail section and txtElementID.Enabled to be True on row 2.
Can anyone confirm or deny that this can be done and, if it can be done, how you would suggest it be accomplished? No matter which way this goes, thanks for help.
You cannot do it through VBA like you did, you need to use Conditional formatting, there you have an option to set the Enabled property.
try in Form_Current() event like
Private Sub Form_Current()
If Me.cboElementType = "Contract Shrink" Then
Me.txtElementID = ""
Me.txtElementID.Enabled = False
EndIf
end sub
I have been searching for days for how to access an individual record on continuous form and I am willing to say it is not possible, but I have a trick that I will share here. I have an investments database, user gets in and writes a proposal and then there is a meeting where either the project is approved (given money) or it is cancelled. However, there are many more states a project can be in, Proposal, Execution, etc. but only Approved/Cancelled can happen at this stage. I created a MockBool field in the Project table. I put that on the continuous form and when the form is closed I run this:
rs_frm=me.recordset
rs_frm.movefirst
while not(rs_frm.eof)
if rs_frm("MockBool") then
rs_frm.edit
rs_frm("ProcessStatus")="Cancelled"
rs_frm("MockBool")=false
rs_frm.update
end if
rs_frm.movenext
wend
I had days of searching and had an epiphany so I thought I would share.

How to handle Access "Run-Time Error 3021 - No current record" after DoCmd.Requery?

I have been having a quite annoying issue on my access project.
It is an Access 2007 - 2016 file.
My database has two queries that I intend to use to filter data to feed combo-boxes.
On one of my forms I have done that with success. The first list is fed from a table, and the succeeding ones are fed by filtering their respective table based on the previous list selection.
Here is a screenshot of my form that worked out.
And here is part of the code that goes with it:
Private Sub frm01ListSetor_Click()
DoCmd.Requery (qry01_frm01ListArea)
Me![frm01ListArea] = Null
DoCmd.Requery (qry02_frm01ListEquip)
Me![frm01ListEquip] = Null
Me![frm01TextID] = Null
End Sub
Private Sub frm01ListArea_Click()
DoCmd.Requery (qry02_frm01ListEquip)
Me![frm01ListEquip] = Null
Me![frm01TextID] = Null
End Sub
Here are the queries that goes along with the code above:
qry01_frm01ListArea
SELECT tblAreas.ID_Area, tblAreas.NomeArea
FROM tblAreas
WHERE (((tblAreas.SetorArea)=[Forms]![frm01_AddEquip]![frm01ListSetor]));
qry02_frm01ListEquip
SELECT tblEquipamentos.ID_Equipamento, tblEquipamentos.NomeEquipamento
FROM tblEquipamentos
WHERE (((tblEquipamentos.SetorEquipamento)=[Forms]![frm01_AddEquip]![frm01ListSetor]) AND ((tblEquipamentos.AreaEquipamento)=[Forms]![frm01_AddEquip]![frm01ListArea]))
ORDER BY tblEquipamentos.ID_Equipamento;
The code above works just fine. It filters data to previous selection accordingly and it also clears the data and selection of the "righter" lists every time another value is selected on the "lefter" ones.
So that was a form I created to help me out on populating my equipment database.
Let us get to the problematic twin now:
I created another form for Maintenance Orders of these equipment.
This one must be more user friendly since other people will use it. Therefore I used Combo-boxes for value selection instead of List Boxes. I say this before hand because it is the only thing I could notice that differs the new form from the one above.
Here I have this:
Screenshot of troubled form
After the expanded Combo-box on image 2 is selected I should run the following code:
Private Sub frm02SetorOM_Click()
DoCmd.Requery (qry04_frm02CBArea)
Me![frm02AreaOM] = Null
DoCmd.Requery (qry05_frm02CBEquip)
Me![frm02EquipamentoOM] = Null
End Sub
Private Sub frm02AreaOM_Change()
DoCmd.Requery (qry05_frm02CBEquip)
Me![frm02EquipamentoOM] = Null
End Sub
It is basically the very same thing I have done on the first form but with different variables.
But I get a "Run-time error 3021: No current record" every time I select a value on my "Setor" field.
The crash happens at the "DoCmd.Requery (qry04_frm02CBArea)" line.
Here is the query for this form's Area field (qry04_frm02CBArea):
SELECT tblAreas.ID_Area, tblAreas.NomeArea
FROM tblAreas
WHERE (((tblAreas.SetorArea)=[Forms]![frm02_SubmitOM]![frm02SetorOM]));
But the thing that is confusing me the most is that when I click "End" on the Run-time error dialog box, my Area combo-box - that was empty before - even with the error gets the proper options like if it actually "requeried".
How can I fix this issue?
Thank you in advance.
Btw, this is my first Access project so please consider that on the response.

Select combobox intem after bounded with SQL Query in Access

I have the following pair of combobox that are used for two inserts in two different access tables from a single form.
The problem I have is that I am not able to make anything else load the form is selected both in Name_OT and in Year the first value that contains corresponding combobox.
I think the solution is with:
Combobox1.Selected (0) = True 'First value
But the combobox goes blank, no text or anything appears.
Solved with this
Cuadro_combinado79 = Cuadro_combinado79.ItemData(0)
Cuadro_combinado85 = Cuadro_combinado85.ItemData(0)

MS Access - Disable Update on a Certain Field or Column

How do we disable update or edit on a certain column for a record. For example I have Product table with fields of ID, Description, Count. I want to disable the changes on Description only. I know how to do this in sql, but how about in a program for Access or VBA code?
If you're using a form to present the data using a text box control bound to the field, you can lock the field for edits in the field properties. The property is called "locked".
You can also use vba code to lock and unlock the field under certain conditions.
Sub form_current ()
If x = "superuser" then
Me!SalaryField.enabled=true
Me!SalaryField.locked=false
Else
Me!Salaryfield.enabled=false
Me!Salaryfield.locked=true
End if
End Sub

Microsoft Access, auto generate columns in DataSheet subform

I want to create a form in MS Access 2003 that lets the user pick from any existing query, and then have it display the results inside the form (as a sub-form in DataSheet view). The user will then be able to select one or more records and click a button on the parent form to do certain actions based on the selection. I want it to be able to work with any query, with very few limits, and display the full results of the query (all columns). The only requirement I might have is that it include certain fields for certain actions. For example, if I have a "send email" action, the query will require a field named "email", or maybe "to" and "subject".
Changing the DataSource of the DataSheet sub-form at run-time isn't a problem, I've done that before using VBA. Getting the columns displayed to change is the problem.
In a .NET WinForms app this could be done with the "auto generate columns" on a GridView control, or using the GridView.Columns collection directly in code. In VBA I don't see a way to add/remove columns from a DataSheet view. I also don't see a way to auto generate them based on the query. It appears the columns are controlled by the controls placed on the form (in form view), and while it is possible to add/remove controls using VBA, the form would have to be placed in Design View and require exclusive access to the database -- sounds very messy and I would like to avoid the exclusive access part.
Am I missing something? Is there an easy way to do this?
Here's how I would go about it. Create a blank subForm control on your main form. To change the source and the columns just leave the source object blank, then when you set it with code, the columns will reset to whatever source you use. So set it like so:
Private Sub setSource()
Me.subForm.SourceObject = "Query.myQuery"
End Sub
Then to get the selected items, assuming you know what column you want, you would do something like this:
Private Sub getSelected()
Dim rs As Recordset
Dim f As Form
Set f = Me.subForm.Form
Set rs = f.RecordsetClone
Debug.Print f.SelTop
rs.MoveLast
rs.MoveFirst
rs.Move f.SelTop - 1
Debug.Print rs!ID
End Sub
If you don't know the column explicitly you can use this to loop through the columns of the selected item and run some analysis on each name until you determine it's the column you want.
Dim i as Integer
For i = 0 To rs.Fields.Count - 1
Debug.Print rs.Fields(i).Name
Next