I use a report in MS Access which displays records out of a database based on dates (given by the user).
I seem to having some trouble using my If Else statement when the Report is loading. I have one row in the database named BDatGift90. Sometimes, this value may be empty for a record. In that case, I want to use the If Else statement.
My If Else statement is:
Private Sub Report_Open(Cancel As Integer)
If IsNull(BDatGift90.Value) Then
Me.GBedrag10.Visible = True
Else
Me.GBedrag.Visible = True
End If
End Sub
The report is not loading at all. The text boxes GBedrag and GBedrag10 are not visible and I would like to use my If Else statement to decide what text box has to be visible.
Can I use some error reporting somewhere or is my if else wrong? I could use some explanation from somebody.
You check the BDatGift90 while the report is opening. But is BDatGift90 one column in the recordsource of the record? If yes, which row of the recordsource is being checked by your if else statement? I'm asking because Report_Open() is called only once. In contrast, an event like GroupHeader1_Format()is called several times, and the values are stepping forward through the recordsource each time.
So, one approach is to clarify the life cycle of BDatGift90. Another approach is to set a breakpoint and to inspect BDatGif90 (VBE > Debug > Add Watch...). Is it null, is it empty, nothing or ""?
Here is an interesting check for zero-length string in textboxes: Proper way to check if an unbound control has a value
And here are more hints about isEmpty(), isNothing() etc. http://allenbrowne.com/vba-NothingEmpty.html
Hope this helps :-)
Corresponding to your question, try this:
Private Sub Report_Open(Cancel As Integer)
If BDatGift90.Value Is Nothing Then
Me.GBedrag10.Visible = True
Else
Me.GBedrag.Visible = True
End If
End Sub
Related
What I'm trying to do is, whenever a user opens a form (and the sub-form that opens by default), I want to search through all the columns (controls?) on the form, check to see if they are currently set to aggregate (sum, count etc.) with Access' built-in Totals row, and if so, set them to not aggregate.
The reason for this is there are several millions records that are stored, so when someone queries it down to 3-4 and turns on Sum, then closes it, when the next person opens it, it tries to sum millions of numbers and freezes up. The form displays the queried results from a table which is populated via SQL (I think, if that sentence makes sense). Here's what I have so far:
Private Sub Form_Load()
'this form_load is in the UserApxSub sub-form, for reference
Call De_Aggregate
End Sub
Private Sub De_Aggregate()
Dim frm As Form, con As Control
Set frm = Forms!UserAPX!UserApxSub.Form!
For Each con In frm.Controls
If con.ControlType = acTextBox Then
If con.Properties("AggregateType").Value <> -1 Then
'crashes on following line
con.Properties("AggregateType").Value = -1
End If
End If
Next con
End Sub
I have not so much experience in Access VBA (usually work in Excel VBA) so please forgive me if I'm entirely off the mark here. The command con.Properties("AggregateType").Value = -1 doesn't throw an error, but Access just straight-up crashes when reaching that line specifically.
I've tried a number of variations in the syntax with no success, and I've also tried looping through other elements of the file (tabledefs, querydefs, recordsets, etc.) as well to see if I'm trying to change the wrong value, but the controls on this subform are the only things in the entire .mdb file that results when I search for elements with the AggregateType property.
I switched out the line that errors with Debug.Print con.Name & " - " & con.Properties("AggregateType").Value and I can check, have nothing return anything other than -1, turn on aggregation in some column manually, and have it return the correct result (0 for sum for example), so I think I'm looking in the right place, just missing some key factor.
I've been working on this for a couple weeks with no success. Any way to fix what I have or point me toward the right direction would be greatly appreciated!
This is not necessarily the answer but I don't have enough reputation
to give a "comment"...
I tried your scenario and verified can change the property value as you are however I did not iterate through all controls and simply used an onDoubleClick event on a column to simulate.
I would suggest trying to fire your sub with Form_Open or Form_Current to see if the property is getting reset after your code has been called for some reason.
UPDATE:
You are referencing the "Subform" Object of your main Form:
Set frm = Forms!UserAPX!UserApxSub.Form!
Try referencing the actual UserApxSub FORM explicitly.
Something like Set frm = Forms!UserApxSub! (assuming UserApxSub is the name of the form)
then stick in the Form_Open of your main form:
Private Sub Form_Open(Cancel As Integer)
'// the following would set a single control only. You can add your loop through all controls
Me!{your control name}.Properties("AggregateType").Value = -1 '// set control in your main form
Form_UserApxSub!{your control name}.Properties("AggregateType").Value = -1 '// set control in your "sub" form
End Sub
I am having a trouble while trying to conditionally format the exhibition of records in a report inside a MS Access 2007 form.
I have search the Internet and I have seen lots os fellows stating that it is possible to perform visual changes in a single record via code implementing the method Detail_Paint() for the event Paint of the Detail section in a Report. Those people say that something like this is going to work:
Private Sub Detail_Paint()
val = CStr(Me.someTextBox.Value)
If val = "constraint" Then
Me.lineStrikethrough.BorderStyle = 0
End If
End Sub
The problem is that although the reading statement Me.someTextBox.Value returns the value of each record when the Paint event is thrown, the writing statement Me.lineStrikethrough.BorderStyle = 0 writes the value of the property BorderStyle for every single line in my report, not only for the one respecting the single record whose value I read from someTextBox field.
Can anyone tell me why such is happening? If this is the correct behaviour (although it does not seem right to me), how can I achieve my goal?
Note: lineStrikethrough is used to perform a strikethrough effect over the record in a Report. If there is another way to do that I would be happy to know.
Two things:
1 - Use the Detail's On Print event and not On Paint event.
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
val = CStr(Me.someTextBox.Value)
If val = "constraint" Then
Me.lineStrikethrough.BorderStyle = 0
End If
End Sub
2 - To see the conditional formatting, always open your report in Print Preview and not Report View.
You are really close!! :-) In my testing, it appears that the Detail_Paint() event allows you to change the formatting of controls, but changes will continue in subsequent records until you change/reset them to something else.
In your code sample, you just need to add another line to turn the strikethrough BorderStyle back on when the condition is no longer met.
Private Sub Detail_Paint()
Val = CStr(Me.someTextBox.Value)
If Val = "constraint" Then
Me.lineStrikethrough.BorderStyle = 0
Else
Me.lineStrikethrough.BorderStyle = 1
End If
End Sub
I found this clue on a code sample on MSDN which demonstrates conditional formatting using the Detail_Paint() method.
I found that Select Case to work best in the detail paint over if statements for a continuous form.
'Private Sub Detail_Paint()
Select Case CStr(Me.someTextBox.Value)
Case "constraint"
Me.lineStrikethrough.BorderStyle = 0
Case Else
Me.lineStrikethrough.BorderStyle = 1 'or what the default value is
End Select
Good afternoon,
I've got a continuous form that displays records for any number of selected employees. I'd like for only that employee's row (or more specifically a particular text box in their row) be editable and no others.
I thought about doing something like this.
Private Sub Form_Load()
If Me.txtResponse <> [Forms]![Home].txtEmployeeName Then
Me.txtResponse.Locked = True
End If
End Sub
and I get an error that I entered an expression that has no value - and it highlights the me.txtResponse.
I don't know if i'm barking up the wrong tree or if this is even possible in a continuous bound form. Any ideas?
Form_Load is too early for that code. Move it to the Form_Current event and it will run when first opened and again with each record navigation. You'll want to add
Else
Me.txtResponse.Locked = False
in their to allow changes when a match is made.
I am building a form In MS Access for users to input data but there are too many possible fields. Most of the time only about half the fields will be used.
I thus would like to have certain fields appear only depending on what the user inputted on a prior given field.
Ex: user enters project number, title, then he checks a "yes/no" engineering. since he checked it this means engineering is impacted so a dozen fields that the user will have to fill out appear.
Is this possible:
1)without VBA
2)with VBA
Probably not possible without VBA.
With VBA for example:
Ensure your form is in Design view
Right click on your Combo Box, Build Event, Code Builder
This opens the code behind your form. It drops you into the default code for the BeforeUpdate event. We want the Change event instead, so at the top right change the drop down from BeforeUpdate to Change. This will give you a bit of code like this:
Private Sub Field1_Change()
End Sub
Inside here, you want to check the value of the combo box and hide fields as required:
Assuming the name of your combo box is Field1 (yours of course will be different), you add some code so it looks like this to hide Field2:
Private Sub Field1_Change()
If Field1.Value = "Yes" Then
Me.Field2.Visible = False
End If
End Sub
Note you need to know the names of all your fields - this is in the Other tab, Name field in the properties box (F4). You should give all of your fields sensible names so you can understand what is going on in the code.
For a check box, follow exactly the same procedure, but you probably need to use the Click event. Just experiment.
Sample check box code:
Private Sub Check5_Click()
' Note: vbTrue = -1
If Me.Check5 = vbTrue Then
MsgBox ("Ticked")
Else
MsgBox ("Not Ticked")
End If
End Sub
I have a form that will show certain fields after a list box value is selected. I use the AfterUpdate function. It has worked so far. My code is below. ProjectName and ProjectNumber are fields you only want displayed if Engineering is selected. OtherName and OtherNumber are fields you only want to show if it is a "NotEngineering" project.
Insert this code by clicking on the Field that selects the project type, go to the Event tab on the property sheet, and click "After Update" and choose code builder and paste in VBA.
Private Sub ProjectType_AfterUpdate()
If ProjectType.Value = "Engineering" Then
Me.ProjectName.Visible = True
Me.ProjectNumber.Visible = True
Else
Me.ProjectName.Visible = False
Me.ProjectNumber.Visible = False
End If
If ProjectType.Value = "NotEngineering" Then
Me.OtherName.Visible = True
Me.OtherNumber.Visible = True
Else
Me.OtherName.Visible = False
Me.OtherNumber.Visible = False
End If
End Sub
There is a way to do not-quite-this without VBA. I'd definitely recommend VBA though, because you can do a lot more with it.
Rather than hiding, try disabling the unnecessary fields with conditional formatting by expression.
right-click on the control you want disabled.
go down and click on 'Conditional Formatting'
Add a new rule
Select 'Expression is'
example:
[fld1]="yes"
hit the disabled box
click ok
click ok
now the control you've selected will disable if field 1 has "yes" selected.
I have the same problem and I did the following:
Private Sub Field1_Click()
If Field1 = "Yes" Then
Me.Field2.Visible = True
Else: Me.Field2.Visible = False
End If
End Sub
but now I have other problem, when I change record, the field that I choosen to be visible in the last record is now visible on the current record, although I have not choosen any option.
Thank you,
here is my situation:
I have a report that is displaying several values like the expected value and the obtained value. I would like to display an alert when the expected is over the obtained. This kind of manipulation is straightforward in a form:
If expectedCtrl > obtainedCtrl Then alertCtrl = "PROBLEM"
The problem is that in a report, its continuous aspect makes it impossible to get the expectedCtrl and obtainedCtrl values that way. Does anybody know the right way to perform this simple task ?
Thank you very much for your help, I'm really stuck on it and I don't have much time...
Just create an unbound control and set an expression for the control data source. Something like this...
=iif(expectedCtrl>obtainedCtrl, "PROBLEM","")
Alternatively you can use the Conditional Formatting feature and define a condition to change the color, for example, of the obtainedCtrl field if it is greater than the expectedCtrl.
I believe you need to set up some code for one of the events in the Detail section of the report. In the report's design view, right-click 'Detail', then properties, and check its events. This will allow you to run code for each detail record that shows up on the report.
You need to include both If and Else. For a picture, I suggest you set the visible property, say:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If expectedCtrl > obtainedCtrl Then
Me.PicAlert.Visible = True
Else
Me.PicAlert.Visible = False
End If
End Sub
Or more simply:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.PicAlert.Visible = (expectedCtrl > obtainedCtrl)
End Sub