Hiding Null Records on Command - ms-access

My question is fairly straight forward and hopefully simple.
I've got a database that has a long list of part numbers, and some items without part numbers. What i'd like to do is have a command button or Radio button that when activated, hides these null records, and when pressed again, shows them once more.
I can give more detail if needed
Thanks for any help!

Here is an example of using the Filter (same as using of out-of-the-box filtering capability from the Access/Office menu):
Setup:
Table (e.g. Parts):
Form (PartsForm):
Event handler for the Checkbox (OnClick):
Private Sub DisplayAllFilter_Click()
Me.Filter = ""
If Me.DisplayAllFilter.Value = False Then
Me.Filter = "[PartNumber] is not null"
Me.FilterOn = True
Else
Me.FilterOn = False
End If
End Sub
Result would be something like these:

Related

If value is nothing, do something

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

Make fields visible in MS Access form as certain fields are completed

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,

MS Access TextBox Value is always NULL

I am doing some work for someone that insists on using MS Access. I don't usually use it so I am a bit new to the whole control structure and best practices. What I am trying to achieve is to have a filter textbox on a form which, when a value is entered, it will filter the rows in the detail section. That seems like a straightforward use case. I initially tried the following behaviour as the event handler for the On Change event:
Private Sub FilterGrid()
Me.Text32.SetFocus
If Not IsNull(Me.Text32.Text) And Me.Text32.Text <> "" Then
Me.Filter = "JobNumber LIKE '*" & Me.Text32.Text & "*'"
Me.FilterOn = True
End
Else
Me.FilterOn = False
End If
End Sub
This worked perfectly until I typed something that didn't have any rows matching and the whole thing exploded with this error (and was unrecoverable without closing the form):
Run-time error '2185': You can't reference a property or method for a control unless the control has the focus.
I did some reading around and the general opinion was that .Text should not be used and .Value (or simply the Text32 without a property) should be used. That produced some very strange behaviour. The Text32.Value is ALWAYS null. I have a watch window and I can see that for the normal behaviour, Text32.Text has an actual value, but Text32.Value is NULL.
Obviously I am doing something wrong, but I don't have enough experience with Access to know what it is.
Just as an aside, another suggestion was to do Text32.SetFocus right before accessing the Text property. This doesn't resolve the error I mentioned. It still throws the exact same error.
Is anyone able to point me in the right direction here?
As you found out, the textbox's Value is only set after the control loses focus.
Conversely, the Text property is only accessible while the control has focus.
The Value property is defined as the default member for controls; that means Text32 will be implicitly the same as Text32.Value, however, depending on the context,Text32 can sometimes refer to the control itself, not just its value.
All these discrepancies can sometimes be infuriating.
To go back to the matter at hand: you have 2 ways to handle filtering.
if the list to filter is large, it's probably better that the user type their filter, then press ENTER to validate it.
if your list is not too large, you can implement filter as you type.
First case, wait for user input to be validated by ENTER.
Say your filtering textbox is called txtFilter and is located on a form whose subform is showing a datasheet (or continuous form) that you want to filter.
All you need to do is wire the textbox OnKeyDown events as such:
' We will only perform the filter if the user press the ENTER key
Private Sub txtFilter_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 13, 9
KeyCode = 0
QuickFilter
End Select
End Sub
' Perform the actual filtering on the subform
Private Sub QuickFilter()
Dim sql As String
Dim filter As String
If txtFilter.Text = vbNullString Then
' Reset the filter if the textbox is emtpy
SubForm.Form.FilterOn = False
Else
'Some common substitutions that users may have already inserted as wildchars
filter = Replace(txtFilter.Text, "%", "*")
filter = Replace("*" & filter & "*", "**", "*")
' We construct the filter SQL
sql = "([JobNumber ] LIKE """ & filter & """)"
sql = sql & " OR ([ProjectCode] LIKE """ & filter & """)"
sql = sql & " OR ([SupplierName] LIKE """ & filter & """)"
'... Add as many columns to filter on as you want
' Assign the filter to the subform
SubForm.Form.filter = sql
SubForm.Form.FilterOn = True
End If
End Sub
Second case, filter as you type
Well, it's fairly easy, we just need to add to the above solution a way to track changes as the user is typing.
This is best done through the OnChange event of the texbox.
Private Sub txtFilter_Change()
QuickFilter
End Sub
That's all you need to add.
dont use .text property
instead use .value property
text3.value=text1.value+text2.value
you dont need setfocus like .text property everytime.
it works perfectly....vba ms access

Textbox to filter datasheet (finds matching records with each keystroke you type) in MS Access

Can someone help me understand why this code isn't producing expected results?
I have a form with a text box called TxtVendorSearch.
When a user begins typing into this text box, I'd like for it to start filtering the results in the form's subdatasheet.
I am starting with a fully populated datasheet, and I'm not sure why the filter blanks out the whole thing as soon as I start typing a valid filter that should leave results.
Private Sub TxtVendorSearch_KeyPress(KeyAscii As Integer)
Dim str1 As String
str1 = "[VendorID] LIKE '*" & Me.TxtVendorSearch.Value & "*' "
Me!subOrderDS1.Form.Filter = str1
Me!subOrderDS1.Form.FilterOn = True
End Sub
I had a similar problem and i searched the internet for the keyword "find as you type" AND "ms access"
i found this great article. hopefully it will solve your problem.
N.B. this article also contains the source code to use.
Do not use the KeyPress event, you're not going to be able to easily reconstruct the actual input data from within the event handler itself (when the event is triggered, the key just pressed is not yet added to the text of the textbox, meaning you're always short a the last keystroke).
Instead, use the KeyDown event, and use the textbox' .Text property instead of .Value.
.Value is only set once the focus has moved away from the box.
So your code can be simply rewritten as (make sure your KeyDown event is set in the textbox's events on the form):
Private Sub TxtVendorSearch_KeyDown(KeyCode As Integer, Shift As Integer)
' If there is no filter criteria, disable filtering '
If Len(Trim(TxtVendorSearch.Text)) = 0 Then
subOrderDS1.Form.FilterOn = False
Exit Sub
End If
' Do nothing if user hits RETURN or TAB '
If KeyAscii < 32 Then
KeyCode = 0
Exit Sub
End If
Dim str1 As String
str1 = "[VendorID] LIKE '*" & Trim$(TxtVendorSearch.Text) & "*'"
subOrderDS1.Form.Filter = str1
subOrderDS1.Form.FilterOn = True
End Sub
I used Trim() to remove any leading and trailing white-space that the user may have typed.
Last thing: you don't need to use Me. or Me! from within the form's code itself.
Doesn't hurt if you do, but it makes things a bit less legible without adding anything to the code.

How to set a textbox to enabled on event

I have a textbox on an access form that I would like to enable for edditing if the textbox is blank. I am new to VBA, and am uncertain of the best way to go about it. Using a double-click event would be fine with me, but I would prefer to do it on load.
I have tried the following code, but can't seem to get it to work. Any help would be appreciated.
Private Sub EmpID_DblClick(Cancel As Integer)
If EmpID.Text = "" Then
Me.EmpID.Enabled = True
End If
End Sub
For things that should happen on a per record basis, you are best off using the current event. Do not use the .Text property for controls. If you must use a property, use .Value. The .Text property is only available when the control has focus and is generally only used in special cases such as the Change event.
Private Sub Form_Current()
If IsNull(EmpID) Then
Me.EmpID.Enabled = True
End If
End Sub
In a normal set-up, there is no chance of the control being equal to a zero-length string ("")
You can cover all bases by saying:
Trim(EmpID & "") = ""
You are running into the Null problem
Null is not the same as ""
Try this instead
If EmpID.Text Is Null Then
Me.EmpID.Enabled = True
End If