Button click unclick ms-access - ms-access

In access form, I'm hoping to click on a button and add information to an existing record using an update query. Ideally, when this happens, the button will change colors and appear 'activated'.
Then, if the user decides that the information that has been added needs to be removed, they can click the same button again. This removes the previously added information from the table and changes the button appearance to 'inactive'

You can't change the colour of a command button if you are using Accesss 2003 or earlier, but you can simulate a button using a label, and obviously you can change a labels caption and colours.
If you are using 2007 on-wards then substitute the Label name to your Command button name.
Using the On_current property of your form use something like
If Me.AddData = 1 Then
Me.YourLabel.Caption = "Remove Data"
Me.YourLabel.BackColor = VbRed
Else
Me.YourLabel.Caption = "Add Data"
Me.YourLabel.BackColor = VbGreen
End If
Then use a similar logic to run your update code from the On_click property of the label, based on the value of AddData.

If you're willing to have -1 and 0 in AddDate these can be easily converted to TRUE/FALSE.
This is taking a few liberties with the info you've given, but it's getting late in the day.
Your form has a command button (Command7) and a textbox (Text8).
Text8 has a control source linked to AddData (so it shows -1 or 0).
This code executes whenever you move to a different record. It checks the value in Text8 and changes the colour of the command button accordingly:
Private Sub Form_Current()
With Me
Select Case .Text8
Case -1
.Command7.BackColor = RGB(0, 255, 0)
Case Else
.Command7.BackColor = RGB(0, 0, 255)
End Select
End With
End Sub
This code on the click event of the command button will change the value in Text8 from True to False and vice versa.
It will then requery the form, forcing the Form_Current event to fire.
A requery moves the recordset back to the first record, so the bookmark moves it back to the record you were looking at.
Private Sub Command7_Click()
Dim bkmrk As String
bkmrk = Me.Bookmark
With Me
.Text8 = Not CBool(.Text8)
.Requery
Me.Bookmark = bkmrk
End With
End Sub
Edit:
Scrap that -1 and 0 malarkey....
Change the Select Case to Case 1 in the Form_Current event.
Change the .Text8 = Not CBool(.Text8) in the Command7_Click event to
.Text8 = Abs(Not CBool(.Text8 * (-1)))

Related

How to short the code to change control properties on command buttion click in ms-access vba

I have a userform with a group of command buttons with similiar properties and fuctionality. I am using these buttons to let the user keep track about his activity. So e.g. whenever user will click the "production" button the application label will say "production" mode, when the user presses "Break" button the label will change from "production" mode to "Break" mode, Similiarly I have 7-8 buttons altogether on same form. Other conditions are whichever buttons gets pressed that button should get disabled and all other buttons should get enabled, followed by next button, if pressed it should get disabled and it should enable the button which was pressed previously.
The button which has been pressed whould also change its color and should also change the previous button's color back to normal.
I understand it would be difficult to understand the scenario, please check the code below for one of my buttons and its working well.
Private Sub btn1()
Me.Label78.Caption = Me.btn1.Caption
Me.btn1.BackColor = RGB(250, 100, 100)
Me.btn1.Gradient = 12
Me.btn1.Enabled = False
Me.btn2.Enabled = True
Me.btn2.BackColor = RGB(100, 250, 100)
Me.btn2.Gradient = 12
Me.btn3.Enabled = True
Me.btn3.BackColor = RGB(100, 250, 100)
Me.btn3.Gradient = 12
End Sub
the above code works perfeclty and changes properties to 3 buttons(its sample code not the code for all 7 buttons), but I have total 7-8 button and i may add more buttons in future SO if i keep on writing this kind of code in one button for all 7-8 buttons, then it will take a lot of time and efforct.
So what is the best possible way to short this code ?
I know this is little complicated hence please ask if any more information required.
Thanks in advance !!
You can have a helper subfunction:
Private Sub SetButton(ByVal ButtonId As Long, ByVal Enabled As Boolean)
Dim BackColor As Long
If Enabled Then
BackColor = RGB(100, 250, 100)
Else
BackColor = RGB(250, 100, 100)
End If
With Me("btn" & Cstr(Id))
.Enabled = Enabled
.Gradient = 12
.BackColor = BackColor
End With
End Sub
Then call this in the OnClick event like:
For Id = 1 To 8
Select Case Id
Case 1, 2, 4, 7
SetButton Id, True
Case Else
SetButton Id, False
End Select
Next

Why won't the ForeColor of this label visibly change when altered in a loop?

I am making a form that has groups of controls that I want to visibly enable/disable with a related toggle button (let's call them Group Toggles). Each group has a different variety of control types, so I made a common procedure to handle the toggling:
'constants for control ForeColors
Public Enum LabelForeColor
Default = 8355711
Off = 14277081
End Enum
Public Enum ListForeColor
Default = 4210752
Off = 12566463
End Enum
Public Sub EnableControl(Ctrl As Control, Enabled As Boolean)
With Ctrl
Select Case Ctrl.ControlType
Case acLabel
If Enabled Then .ForeColor = LabelForeColor.Default Else .ForeColor = LabelForeColor.Off
Debug.Print "LABEL", .ForeColor
Case acListBox
If Enabled Then .ForeColor = ListForeColor.Default Else .ForeColor = ListForeColor.Off
.Enabled = Enabled
Debug.Print "LIST", .ForeColor
Case acCommandButton
.Enabled = Enabled
Debug.Print "BUTTON", "NA"
Case acCheckBox
.Enabled = Enabled
Debug.Print "CHECK", "NA"
Case Else
Debug.Print "Control [" & .Name & "] is not of a type that EnableControl can handle."
End Select
End With
End Sub
Each group of controls is represented by a collection. When the form is loaded, every control with a particular tag property is added to the corresponding collection. The Group Toggles are not added to any collection and instead have event procedures that look like this:
Private Sub ToggleGroup1_AfterUpdate()
Dim State As Boolean
'a public function that converts the toggle button's value to a boolean
State = FormCommon.ToggleButtonState(ToggleGroup1.Value)
Dim iCtrl As Control
For Each iCtrl In Controls_ByPlant
FormCommon.EnableControl iCtrl, State
Next iCtrl
End Sub
When I click on a GroupToggle, all of the controls in the corresponding group visibly change appropriately, except for the labels. After an hour of troubleshooting, here's what I know:
The ForeColor property of the label does change, but not visibly.
When I call EnableControl on a label outside of a loop, the label visibly changes.
It doesn't matter if I pass the label object specifically to the subroutine or if I pass it from its group collection; the change is visible in both cases
If I toggle-disabled a label as part of the Group Toggle event and then call EnableControl specifically on that label to try to disable it again, there is no visible change (probably because the ForeColor property is already set to the "off" color)
Turning screen updating off with Application.Echo while the Group Toggle event runs and then turning it back on at the end of the event does not make a difference.
Making the Group Toggle event run with a For i = 1 to .Count instead of a For Each does not make a difference.
This problem also occurs when changing a different visual property instead, such as ForeTint.
(Per comments) Repaint does not make a difference
(Per comments) DoEvents does not make a difference
Why is this happening?
(First ever question, so apologies if I messed something up in the post)
This was interesting, but somewhat anticlimactic.
Your code does work for the labels, but what happens is this:
All labels are associated with input controls (as it is usual)
When you deactivate a group, you disable the input controls (.Enabled = Enabled)
This automatically sets the associated labels to a (system defined) light gray text color which cannot be changed.
This "disabled label" color is very similar to your LabelForeColor.Default color, so it is hard to see the change when toggling. But it does change.
Change your color constants to make the effect more visible:
Public Enum LabelForeColor
Default = vbRed ' 8355711
' the "Off" color is never visible, unless you add an un-associated label to a group
Off = vbBlue ' 14277081
End Enum
Edit: your test code FormCommon.EnableControl iCtrl, False works, because it only affects the label, but doesn't disable its associated list box.

Error 424 when trying to get a value form combobox in ON Activate event

I have been trying use the value in the combo box however the combobox will be locked if the revision num > 0
it gives me an error when i put it in the ON ACTIVATE event
i got the error 424 on Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1).Value
Private Sub Form_Activate()
Me.Requery
' prevents user from changing the client, chemical, active name once the
revision number has become 1 or more
If Me.txtRev.Value > 0 Then
Me.cboClient_Name.Locked = True
Me.cboChemical_Name.Locked = True
Me.cboActive_Name.Locked = True
Me.cboActive_Name.Requery
Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1).Value
Me.txtADE_PDE.Value = Me.cboActive_Name.Column(2).Value
Me.txtTTC.Value = Me.cboActive_Name.Column(3).Value
End If
End Sub
Is there anyway i can solve this problem?
When you requery the combobox, it looses its value (if it is unbound as it seems), and then neither column(1) has a value.
So after the requery, set the combobox to some value - or rethink your concept.
Edit:
Property Column(n) carries no (sub)properties, thus:
Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1)
Me.txtADE_PDE.Value = Me.cboActive_Name.Column(2)
Me.txtTTC.Value = Me.cboActive_Name.Column(3)

ms access vba code I can disable a field based on content of another field, but my code to re-enable is not working

I have a simple form where I have a field InstrumentTypes_ID and a field DGLenders_ID.
When InstrumentTypes_ID equals "Mortgage" or "Modification", I want DGLenders_ID to stay enabled, but any other value should make DGLenders_ID become disabled.
I have the following code in an AfterUpdate for the InstrumentTypes_ID textBox, which to me seems would be sufficient to get this effect (However, when testing, I can type in "Warranty Deed" (or anything that's not "Mortgage" or "Modification"), which disables the DGLenders_ID field, but if I go back and update it with "Mortgage" or "Modification", it won't re-enable that field):
Private Sub InstrumentTypes_ID_AfterUpdate()
Me.DGLenders_ID.Enabled = True
If Me.InstrumentTypes_ID = "Mortgage" Then
Me.DGLenders_ID.Enabled = True
ElseIf Me.InstrumentTypes_ID = "Modification" Then
Me.DGLenders_ID.Enabled = True
Else
Me.DGLenders_ID.Enabled = False
End If
End Sub
You should try to put your code in a subroutine and call it on the current event of the form and in the lost focus event of the field.

moving focus from subform to main form

i have main form MAINF and two subforms SUBONE and SUBTWO
I want to be able to move focus (cursor) between them.
From MAINF to SUBONE or SUBTWO, I can call Me![SUBONE].SetFocus or Me![SUBTWO].SetFocus. This seems to work.
BUT:
1) From SUBONE to SUBTWO, I have no idea. what is the correct way of programatically moving focus?
2) From SUBONE to one of MAINF's control say "Customer ID", how do i do it?
Edit: Here's the code that rtochip provided.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyF5
Me.Parent![Item - Order Subform].SetFocus
DoCmd.GoToControl "Supplier ID NUM"
Case vbKeyF6
Me.Parent.[Item ID].SetFocus
End Select
End Sub
If SUBTWO is a child of SUBONE, then it's the same way. However, if they are siblings thenyou have to reference it as an object on the parent first.
There are two ways to reference objects on your parent:
You can reference the parent
Me.Parent.[Customer ID].SetFocus
(btw, change than control's name to Customer_ID - it makes it easier to use, and you won't require the []'s)
You can reference it directly
Forms!MAINF.[Customer ID].SetFocus
Update: The KeyDown event is probably being caught later on the main form. You could always clear it out before you finish with moving focus.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyF5
Me.Parent![Item - Order Subform].SetFocus
DoCmd.GoToControl "Supplier ID NUM"
KeyCode = 0 'Trap F5
Case vbKeyF6
Me.Parent.[Item ID].SetFocus
keyCode = 0 'Trap F6
End Select
'keyCode = 0 'Note: you can't do it here because it will trap ALL your
'KeyCodes. Not just F5 and F6.
End Sub