Searchable textbox and vba highlights row from continuous form and brings record to top with rest of records to follow - vba

I have a form I want to use as a guide to search for a record then pull the record up to the top form as a highlighted line with the rest of the records to follow. Additionally, I want the searchable text box that I'm searching in to automatically jump to the next record without hitting enter or clearing the textbox. I want that part to be automated. So I would be continuously entering in numbers for thousands of records and looking up their information.
Some other info, my form is non-editable but the text box is.
I've tried using a form with some code but can't get it gives me an error saying my private sub is not recognized as a valid field name or expression.
Private Sub Recsch_AfterUpdate()
On Error GoTo Err_ErrorHandler
Dim ItemSelected As String
ItemSelected = Forms![Inventory_Status].Recsch
Recsch = Null
Me.Filter = "[RecId] = '" & ItemSelected & "'"
Me.FilterOn = True
Err_ErrorHandler:
Exit Sub
End Sub
I just want my code to take me to the next line of the form without having to click anywhere. So it would enter in the data on the box, search in my form, highlight the line, bring it to the top, then highlight my box again so I can enter the next number without pressing enter.

Related

Make the Text box disappear for the current selected record

I have a checkbox in my main form which causes a text box and a button to disappear and appear when it is not checked and checked respectively. The code below works correctly:
Private Sub Check288_Click()
If Me.Check288.Value = True Then
MsgBox "Please Enter Reference Number and fill in the name", , "Database"
Text293.SetFocus
Me.MTM_Signature.Visible = True
Me.btn_8DGen.Visible = True
Else
Me.MTM_Signature.Visible = False
Me.btn_8DGen.Visible = False
End If
End Sub
My problem is this code is affecting the whole list of records instead of the current chosen record. I have an auto numbered primary key (ID) for identifying each records. Is there any way for me to make the text box and button disappear only for the current record. I am new to access VBA. Thank you.
In the event of Check288_Click() display a modal form and let the user enter the Reference Number. When closing the pop-up form update the database. You can use conditional formatting to hide (blend) the text box if you do not want to show it when empty.

Excel vba, unable to select row in listbox

I have a page in a multi page form which takes input from a text box and check box and adds this into a multi column list box below it when a submit button I clicked. I then want the user to be able to select a row in the list box and display the contents in the text box and check box for editing.
I can add multiple text box and check box values into the list box using the submit button, but it does not select a row when I click on the list box item. It just does nothing. It looks like the focus is not being transferred.
Would really appreciate it if someone could give me some ideas or guidance on how I may solve this? The code for the two actions are:
Private Sub cmdaddcontrol_Click()
If Not txtbox_Ctrl_Desc = "" Then 'check for input into text box
ctrl_listbox.AddItem 'add items to list box
ctrl_listbox.List(ctrl_list_count, 0) = ctrl_list_count + 1 'add row number in list box column
ctrl_listbox.List(ctrl_list_count, 1) = txtbox_Ctrl_Desc.Value 'set list box column to text box contents
If Not chkbox_ctrl = False Then 'check if check box is selected then insert appropriate value into listbox column
ctrl_listbox.List(ctrl_list_count, 2) = "Y"
Else
ctrl_listbox.List(ctrl_list_count, 2) = "N"
End If
ctrl_list_count = ctrl_list_count + 1 'increment listbox row counter
txtbox_Ctrl_Desc = "" 'reset text box to blank
chkbox_ctrl = False 'reset check box to blank
Else
MsgBox "You have not enetered anything in the control box" 'error message if no control description in text box
End If
End Sub
Private Sub ctrl_listbox_Click()
Dim i As Integer
'find the selected list item
i = ctrl_listbox.ListIndex
ctrl_listbox.Selected(i) = True
'add the values to the text and check boxes above the list box
txtbox_Ctrl_Desc.Value = ctrl_listbox.Column(1, i)
If Not ctrl_listbox.Column(3, i) = " Y" Then
chkbox_ctrl.Value = True
Else
chkbox_ctrl.Value = False
End If
End Sub
After finding the cause of the issue in the comment chain of the question, I'd like to persist the solution here as an answer for the people that might come across the question in the future:
Make sure that the .Enabled and .Locked properties of the misbehaving ActiveX control are set correctly. (True if the control should allow manipulation/activation by the user at runtime. False if you want to deny the user interaction with the control.)
Verify that the event procedure has the correct name. Maybe you changed a controls name? The correct naming scheme for event procedures is controlName_eventName(), e.g. ListBox1_Click(). An empty _click() procedure is generated when you select Show code in the control's context menu in Design Mode.
Although seemingly trivial, sometimes simple details escape our attention - even more so in complex projects. It's often helpful to have someone from outside the project have look at your problem. In their innocent ignorance of your project's inner workings, they might just be able to ask the right questions that lead to a quick solution.

How to set focus following setting Form Filter that returns zero records

I have a textbox control in the header of a continuous form. Characters entered are used to build and apply a filter string. Once the filter is applied the focus is set back to the textbox using set focus and selstart so the user is able to add more characters. The result is the list of records is filtered as each character is entered.
The set filter code is triggered by the textbox on change event. The code moves focus to another control and then back so that the textbox.value property is updated (I tried using .text but kept running into other focus issues).
The record filtering works until a string is entered that results in zero records being displayed.
At this point the line of VBA that sets the Selstart property throws
"you can't set a property of a control unless it has focus"
The lines of code are listed below, TxtFilterString is the name of the textbox, LengthOfText is an integer, SetFormFilter is a sub that builds the filter and applies it:
LengthOfText = Len(Me.TxtFilterString.Value)
SetFormFilter
Me.TxtFilterString.SetFocus
Me.TxtFilterString.SelStart = LengthOfText
It appears that the line Me.TxtFilterString.SetFocus fails when there are no records to display and this causes the following line to throw an error despite the control being visible in the header section.
I am assuming you have listbox displaying the results of your query.
If it errors when 0 records are displayed in the listbox then could you not put a simple check around the listbox results that doesn't run the line that errors?
if listbox.listcount >0 then
Me.TxtFilterString.SelStart = LengthOfText
endif
I have no idea why the problem occurs but my solution is to not execute the line of code if the record set is empty or the text box in question contains null which seems to happen if the contents are deleted using the delete key. My on change sub is here:
Private Sub TxtFilterString_Change()
' Call sub that sets the form filter to value entered in text box
Dim LengthOfText As Integer
On Error GoTo ErrHandler
Me.CmdResetSearch.SetFocus ' move the focus away to update the value prop, as the text prop has focus issues
Me.TxtFilterString.SetFocus
SetFormFilter ' build and set the continuous forms filter string
' The following if block is to overcome a bug where Access does not allow the
' set focus property of the text field to be set when there are no records to display
' The result is that the filter string that returns zero records becomes highlighted
' allowing it to be overwritten.
' The IsNull clause is required if the user used delete to remove the text in the text box
If Me.Recordset.RecordCount = 0 Or IsNull(Me.TxtFilterString) Then
' There are no records or the user has deleted in the field (causing a Null to be enetered)
Me.TxtFilterString.SetFocus
Else
' There are records and there is text in the text box so set focus and caret position
Me.TxtFilterString.SetFocus
Me.TxtFilterString.SelStart = Len(Me.TxtFilterString.Value)
End If
Exit Sub
ErrHandler:
MsgBox "An error in TxtFilterString_change : " & Err.Description, vbExclamation
Exit Sub
End Sub

Writing values back to a form

I have a form called Main. On it I have
a text box to hold the record ID
a combobox to select from existing records or enter a name for the new record
a text box to hold the name from the combobox
a button to open a form that lets us enter additional data and create a new record
a button to open a form that lets us edit an existing record
I cascade the row source for the list in the combobox. If the entry is not in the list,
the NotInList event
Copies the name in the combobox is moved to a text box on Main
Loads the create form's and copies the name from Main to the appropriate field on the create form.
All that works fine. The problem is that when I hit Save on the create form, I cannot write values back to the Main form correctly. I want to write the name that came from the combobox back to the combobox on Main, and write the record ID to the text field on Main.
It's two lines of code. If I change their order, the output changes. Either the combobox ends up blank, or the record ID text field ends up blank.
While I was debugging this I can see the values exist, but I could not make the assignments. The last time I tried combobox.text it complained about the value not being
in the list, so I ran the query for the row source, but the record did not show up in the combobox list, but is in the table. See the two lines of code (bold) below.
Private Sub cmbSaveClose_Click()
Dim x As Integer
x = MsgBox("Are you sure you want to save changes?", 4, "Exit?")
'VbNo constant throws back #7
'VbYes constant throws back #6
Me.Txt32 = DLookup("InsuranceCarrierContractID", "ICCDupRecordCheckQ")
If x = 7 Then
Exit Sub
End If
If IsNull(Me.Txt32) Then
Me.Txt31 = Form_frmMain.Txt65
DoCmd.RunCommand acCmdSaveRecord
**Forms!frmMain.cboInsuranceCarrierContract = Me.InsuranceCarrierContractID
Forms!frmMain.Txt66 = Me.InsuranceCarrierContractID**
DoCmd.Close
Forms!frmMain!InsuranceCarrierContract.RowSource =
" SELECT InsuranceCarrierContract.ContractNumber " & _
" FROM InsuranceCarrierContract " & _
" ORDER BY InsuranceCarrierContract.ContractNumber;"
'Other stuff,works fine.
Form_frmMain.cboInsuranceCarrierContract.Locked = False
Form_frmMain.cboInsuranceCarrierContract.BackColor =
Form_frmMain.cboInsuranceCarrier.BackColor
Else
Me.Undo
DoCmd.Close
End If
End Sub
This worked for David, so adding it as an Answer:
ReQuery and ReFresh the form after you change the data.
Me.Requery
Me.Refresh
from within the form module, or externally:
Forms("MyFormName").Requery
Forms("MyFormName").Refresh
http://msdn.microsoft.com/en-us/library/office/ff191903.aspx
http://msdn.microsoft.com/en-us/library/office/ff836021.aspx

Placing Label Over Combo-box in Ms-Access

Background:
I am trying to place a label in Ms-Access that is being used as a button over a combo box. The user will select from the combo-box list and if they want to erase their selection they can click on the label to clear the selection.
Question:
Is it possible to get the label to show above the combo? It seems like the label is being sent behind the combo even though I specifically send it to the from etc...
Instead of creating the overlaying label to clear the selection, I would create an actual button. You will stack the button and the combo box on top of eachother in design mode, but set the button visibility to "No" in the format properties. Let's say you call the combo box "cmbSelect" and the clear button "cmdClear", use the following to show the button after the combo box is selected:
Private Sub cmbSelect_AfterUpdate
Me.cmdClear.Visible = True
End Sub
The following code will then clear the data from your table after the button is clicked:
Private Sub cmdClear_Click()
DoCmd.SetWarnings False
'Deletes record from your table
Dim Delete As String
Delete = "DELETE * FROM [TableName] WHERE (([TableName].KeyField)='" & KeyField & "')"
DoCmd.RunSQL Delete
DoCmd.SetWarnings True
End Sub
You could also have the form requery by running a requery on each field instead of using the delete string. Then you could build code for the button "after update" that makes the button invisible again and allows you to select a new entry from the combo box. There's a lot of possibility, but this should get you started. Let me know if you need any more explanation or help.