I'm trying to use VBA to format some unbound textboxes I've added to the rows on a subform set to continuous view.
The VBA looks at the bound text boxes associated with the subform's underlying RecordSource and then formats the unbound textboxes I've added based on the data.
Here's some simplified code:
Public Sub ApplyFormat()
If Forms!tblEnrolments!tblEnrolments_Jobs_sub!Start = 1 Then
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtStart.BackColor = RGB(65, 138, 179)
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtStart.ForeColor = RGB(255, 255, 255)
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtStart = "Start Forms"
ElseIf Forms!tblEnrolments!tblEnrolments_Jobs_sub!Start = 0 Then
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtStart.BackColor = RGB(216, 216, 216)
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtStart.ForeColor = RGB(166, 166, 166)
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtStart = "None"
End If
If Forms!tblEnrolments!tblEnrolments_Jobs_sub!End = 1 Then
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtEnd.BackColor = RGB(8, 164, 71)
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtEnd.ForeColor = RGB(255, 255, 255)
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtEnd = "End Forms"
ElseIf Forms!tblEnrolments!tblEnrolments_Jobs_sub!End = 0 Then
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtEnd.BackColor = RGB(216, 216, 216)
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtEnd.ForeColor = RGB(166, 166, 166)
Forms!tblEnrolments!tblEnrolments_Jobs_sub!txtEnd = "None"
End If
End Sub
Private Sub Form_Activate()
ApplyFormat
End Sub
Private Sub Form_Current()
ApplyFormat
End Sub
So I have a public sub called ApplyFormat that looks at the data in the bound textboxes and then applies formatting (changes the textbox background colour, font colour and text data). This is then called from the Current event (so formats are applied whenever a record is loaded) and the Activate event (because the main form will open other forms whilst still being open and may get focus again when those other forms close).
Unfortunately, records below record 1 in the subform are not being formatted based on data in their record; they are just simply duplicating the formatting applied at the first record.
For example:
Here EnrolID "1" has 2 attached jobs showing in the subform. The first record is formatting correct as per the code above, i.e. the bound "Start" text box is 1 so the unbound text box next to it is being formatted as blue with white text and the string "Start Forms".
In the second record however, the "Start" text box is 0, which should format the unbound text box next to it as grey with the string "None". But as you can see it's just copying the formatting of the first record. Furthermore, the bound text box "End" has a value of 1, which should make the unbound text box next to it green with white text and the string "End Forms".
Is it possible it possible to achieve the effect I'm going for via VBA? I don't think I can use conditional formatting as I want the unbound text boxes to show data such as "Start Forms" and "End Forms" depending on their bound text box counterpart.
You cannot use unbound fields for that: Unbounds fields in continuous forms always have the same formatting in all rows. It's an unfortunate limitation.
Fortunately, there is another option: You can make a bound field, with a ControlSource that calls a user-defined function. For example, in the Form desginer, create a text box and set its "control source" property to = Iif([Start] = 1, "Start Forms", "None") (including the = sign at the start). The formatting would have to be done through conditional formatting.
If you need more complicated logic to determine the text to be shown, you can set the control source to = myCustomFunction([any], [dbfields], [I], [need]) and have the VBA code return the text to be shown.
Related
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
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)))
I add a new line of data into the table through the form. On the form I also have checkbox which is also integrated into the table. Ticking the checkbox leads to getting a value of -1. Is it possible to change -1 into a user defined value such as 'x'?
Simple answer: No.
More complex answer: You can use a hidden bound field, an unbound checkbox, VBA and default values to make a checkbox that behaves just like a bound checkbox that returns a different value.
Consider two fields, myUnboundCheckbox and myBoundTextfield. myBoundTextfield holds "X" for True, "Y" for False.
myUnboundCheckbox is an unbound checkbox, myBoundTextfield is a hidden bound text field
Then you can use the following:
Private Sub myUnboundCheckbox_AfterUpdate()
If myUnboundCheckbox Then
myBoundTextfield = "X"
Else
myBoundTextfield = "Y"
End If
End Sub
Private Sub Form_Current()
myUnboundCheckbox = myBoundTextfield = "X"
End Sub
So, I have a table, 2 forms, a sub form, 2 buttons, and a bunch of text boxes. With these I made a button that pops up an "add field" where you type information into text boxes then click add, and it adds it to the table which back on the other subform shows that data; I have that working. I am not sure how to make it not add the data if you leave ALL fields blank though (note; I only want it to be ALL fields, it's okay if they fill out one field and the rest are null). If all fields are null, it sends out a msgbox to the user saying please fill in data into the text boxes.
Another thing I am looking for for a future part of this database is required fields. Where lets say there are 10 text boxes, and before you can add the data into the table through the form you MUST fill out 6 out of 10 of the text boxes (marked with a * which ones are needed) and if you don't, give them an error saying please fill out [text boxes that weren't filled out] and try again. If all of the 6 required fields were filled out, then it can save it to the table even if the other 4 are null or not.
This is the code I have inside of the add button:
Private Sub CustomerAddBtn_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from CustomersT")
rec.AddNew
rec("CustomerName") = Me.CustomerAddSupplierNameTxt
rec("Address") = Me.CustomerAddAddressTxt
rec("City") = Me.CustomerAddCityTxt
rec("ProvinceState") = Me.CustomerAddProvinceStateTxt
rec("PostalZip") = Me.CustomerAddPostalZipTxt
rec("Phone") = Me.CustomerAddPhoneTxt
rec("Fax") = Me.CustomerAddFaxTxt
rec("CustomerSince") = Me.CustomerAddCustomerSinceTxt
rec("Email") = Me.CustomerAddEmailTxt
rec("Notes") = Me.CustomerAddNotesTxT
rec.Update
Set rec = Nothing
Set db = Nothing
'Send message to user saying it was saved, so they know
Dim intReply As Integer
intReply = MsgBox("Customer has been successfully saved to the database!", vbOKOnly, "Success!")
End Sub
Thanks in advance.
You can check if a Text Box has a value in it by using IsNull(Me.TextBoxName). So, if you want to test if all of the text boxes are empty then you can do something like this
If IsNull(Me.CustomerAddSupplierNameTxt) _
And IsNull(Me.CustomerAddAddressTxt) Then
MsgBox "Please don't try to enter an empty record."
Else
MsgBox "(user filled in at least one field)", vbInformation, "Debug Message"
' your existing database code here
End If
...and just expand the initial If statement to include all of the controls you want to check.
Similarly, for required fields, you can check the value of the controls corresponding to those required fields and display a similar message if any of them IsNull().
The access report has the following script.
The report has many records (or rows of data) and each one has the text box CompColor.
So, the report colors all the CompColor text boxes the same, depending on which one you click in the report. How can I make each one correct based on teh value entered in that text box?
Private Sub Report_Current()
Select Case Me.CompColor
Case "Green"
Me.CompColor.BackColor = vbGreen
Case "Red"
Me.CompColor.BackColor = vbRed
Case "Yellow"
Me.CompColor.BackColor = vbYellow
Case Else
Me.CompColor.BackColor = vbWhite
End Select
End Sub
Use conditional formatting.