TxtBox setfocus does not focus correctly - ms-access

I have a textbox for username. The format is firstname.lastname and I am trying to check to see if the box has a "." in the name to validate the string. The messagebox works correctly, but whenever "Ok" is clicked, it still goes to the next textbox instead of going back to the txt_Name to edit the name entry.
Private Sub txt_Name_AfterUpdate()
Dim username As String
username = UCase(Me.txt_Name.Value)
Me.txt_Name.Value = username
If InStr(1, txt_Name.Text, ".", vbTextCompare) > 0 Then
Me.cb_Shift.SetFocus
Else
MsgBox ("Please use the following format:" & vbCrLf & _
"FirstName.LastName")
Me.txt_Name.SetFocus
End If
End Sub

For such kind validations please use BeforeUpdate event and set Cancel variable to True if the value is invalid. You won't need SetFocus in this case

Related

Text box undo not working if there is a duplicate

I am having a form with a textbox and a list box. I want to enter a new record into the textbox to populate the table and listbox (listbox row source is the table). I have written a code to prevent duplicate entry into the table. When there is a duplicate entry I am getting a pop up to alert the user. What is not working is the undo option to clear the textbox. The code is pasted below with some required info. Any help with the code will be appreciated.
Table name: tblNewComponents
Field name: NewComponents
Textbox name: TextCOMPONENTS
Can someone help me? Thanks
Private Sub TextCOMPONENTS_AfterUpdate()
Dim NewComponent As String
Dim stLinkCriteria As String
Dim custNo As Integer
'Assign the entered customer name to a variable NewCustomer
NewComponent = Me.TextCOMPONENTS.Value
stLinkCriteria = "[NewComponents] = " & "'" & NewComponent & "'"
If Me.TextCOMPONENTS = DLookup("[NewComponents]", "tblNewComponents", stLinkCriteria) Then
MsgBox "This Component, " & NewComponent & ", has already been entered in database." _
& vbCr & vbCr & "Please check the component name again.", vbInformation, "Duplicate information"
Me.Undo
end if
exit sub
Just do this to clear the textbox (and set the focus into it)
Me.TextCOMPONENTS.Value = Null
Me.TextCOMPONENTS.SetFocus
instead of Me.Undo.
your error is in your event act, u should keep it on next textbox gotfocus event ,and not in current textbox afterupdate ,
in next text box gotfocus event,and will work perfectly.
many thanks SOF club

MS Access Combobox not taking a value after requery shows value

I have Combobox that lets users either edit the related values for this field
- cboBEA using a button. I decided to use a NotInList routine whenever they want to ADD a new value. The edit part works without a hitch, but the NotInList part needs to accept a value after providing related fields in a popup form called frmBEA_JDIR
At first the requery of cboBEA wasn't working, so I did a more deliberate resetting of the rowsource by first setting it to "" then the actual SQL of the rowsource.
Here is the code in the "Save" button of the popup form frmBEA_JDR
Private Sub cmdSave_Click()
Select Case Me.OpenArgs
Case "Edit"
DoCmd.Save
DoCmd.Close acForm, "frmBEA_JDIR"
With Form_sfm_AddSPDistro
.cboBEA.Requery
.cboBESA.Requery
.cmbPROGRAM.RowSource = ""
.cmbPROGRAM.RowSource = "SELECT * FROM qLU_BEA_JDIR;"
.cmbPROGRAM = .cboBEA
End With
Case "AddNew"
Dim strSQL As String
strSQL = "SELECT LU_BEA_JDIR.ID, LU_BEA_JDIR.BEA, LU_BEA_JDIR.BESA, LU_BEA_JDIR.ORGANIZATION " _
& "FROM LU_BEA_JDIR;"
With Form_sfm_AddSPDistro
'cboBEA.Requery doesn't work, so...
.cboBEA.RowSource = ""
.cboBEA.RowSource = strSQL
.cboBESA.Requery
.cboBEA.Value = Me.txtBEA
.cmbPROGRAM.RowSource = ""
.cmbPROGRAM.RowSource = "SELECT * FROM qLU_BEA_JDIR;"
.cmbPROGRAM = .cboBEA
End With
DoCmd.Close acForm, "frmBEA_JDIR"
End Select
End Sub
Here is the NotInList event of the calling form:
Private Sub cboBEA_NotInList(NewData As String, Response As Integer)
Dim MsgBoxAnswer As Variant
Response = acDataErrContinue
Me!cboBEA.Undo 'Used this to prevent the requery error caused by frmBEA_JDIR
MsgBoxAnswer = MsgBox(NewData & " is not in the list. Do you want to add it?", vbQuestion + vbYesNo, "Add " & NewData & "?")
If MsgBoxAnswer = vbNo Then
Me.cboBEA = Null
DoCmd.GoToControl "cboBEA"
Else
DoCmd.OpenForm "frmBEA_JDIR", acNormal, , , acFormAdd, , "AddNew"
Form_frmBEA_JDIR.txtBEA = NewData
End If
End Sub
So depending on what calls this form - the NotInList or the Edit, I put it in the openargs parameter that calls frmBEA_JDIR. This is how I handle the update in the SAVE button. Again, the edit part works perfectly, but the AddNew from the NotInList event just won't populate cboBEA even after it is requeried and I can see the new value in it.
In brief: Instead of figuring out what to do in the popup with openargs, let acFormAdd do it for you; acFormAdd will open the form to a new record.
Send the new data in openargs.
Open frmBEA_JDIR in dialog mode. It stops the current code until the opened form is closed.
' open frmBEA_JDIR in dialog mode to add the new data.
DoCmd.OpenForm "frmBEA_JDIR", acNormal, , , acFormAdd, acDialog, "NewData"
'data is added, now requery the dropdown
cboBEA.Requery
Fool around with this without the 'case edit' section until you see it working. Then add the edit part using acFormEdit. You can check the datamode of the popped-up form to see if it's add or edit.

VBA Access Textbox returning Null Value

I have a VBA Access userform. In this useform there is a textbox with the name txtSearch_POBOX. I'm trying to get its value using the code below:
Private Sub txtSearch_FirstName_Change()
MsgBox ([Form_Client List].txtSearch_POBOX.Value)
End Sub
But this is constantly returning NULL. When even when there is a value inside the text box. Any ideas?
Your reference is wrong. It should read:
MsgBox Forms![Client List]!txtSearch_POBOX.Value
As it could be empty, you should use:
MsgBox Nz(Forms![Client List]!txtSearch_POBOX.Value)
remember that if you want to intercept the text box value while digiting, until you "validate" the content change (e.g. losing focus) the .Value property is not updated.
For instance I used a text box to make a running filter of a submask: I wanted to filter the submask while digiting.
To do this you need to use the .Text property.
In the following example I make a running filter of a list of Publishers:
Private Sub txtNameFilter_Change()
On Error Resume Next
Dim strFilter As String
If Not IsNull(Me.txtNameFilter.Text) Then
strFilter = "Publisher LIKE '*" + Me.txtNameFilter.Text + "*'"
Me.Filter = strFilter
Me.FilterOn = True
Me.txtNameFilter.SelStart = Len(Me.txtNameFilter.Text)
End If
End Sub
Bye
Wiz

How is this returning a blank value?

So this code is meant to serve as a simple search system to go to certain records in a recordset. I originally had it so they had to click the btnGoToID button in order to perform the search. I decided to just make it a little more user friendly and make it so the search field listened for the Enter button and that would perform the search as well.
The issue that I am running into when the code gets to strID = Trim(Nz(Me.txtSearch.Value, "")) the value will randomly come back as an empty string even though visually I can see that there is a value in the textbox.
I haven't been able to narrow down any pattern for when this issue occurs. At this point I don't really even know how to troubleshoot this problem, nor the words to search for that could yield any results in Google. All I can say is that it SEEMS like changing between records will affect whether or not the search goes through.
This has always worked up until I put in the txtSearch_KeyPress sub procedure.
'============================================================================
' txtSearch_KeyPress
'============================================================================
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
'If user pressed enter
If KeyAscii = 13 Then
Call btnGoToID_Click
End If
End Sub
'============================================================================
' btnGoToID_Click
'============================================================================
' <<Purpose>>
' Allow the user to search for a specific ID
'============================================================================
Private Sub btnGoToID_Click()
On Error GoTo Err_Handler
Dim rs As Recordset
Dim strID As String
Set rs = Me.RecordsetClone
strID = Trim(Nz(Me.txtSearch.Value, ""))
If (strID <> "") Then
'Go to the ID
rs.FindFirst "ID = '" & strID & "'"
If rs.NoMatch Then
MsgBox "ID does not exist"
Else
'If we have a match, set the record as the current record
Me.Bookmark = rs.Bookmark
End If
Else
MsgBox "Please enter a valid ID.", vbOKOnly, "Invalid ID"
End If
Exit_Handler:
On Error Resume Next
Me.txtSearch.Value = ""
rs.Close
Set rs = Nothing
Exit Sub
Err_Handler:
Call LogError(Err.Number, Err.Description, "txtSearch on " & Me.Name)
Resume Exit_Handler
End Sub
After the conversation in the comments I have narrowed down this question to be a whole lot simpler. This yields an "Invalid use of null" error message even after there are several characters in the text field. Why would that happen, and what can I do to make it pick up the values in the textbox?
'============================================================================
' txtUnitNoToSearch_KeyPress
'============================================================================
Private Sub txtUnitNoToSearch_KeyPress(KeyAscii As Integer)
MsgBox Me.txtUnitNoToSearch
End Sub
I think your problem is related to the fact that a text box has 2 properties, .Value and .Text, which appear similar but behave differently.
While you have an edit in progress in the text box, the changing content is available via the .Text property. However, the content of the .Value property has not yet been updated to match.
After the text box's After Update event, .Value will contain the new content. And if focus has moved away from the text box, its .Text property will no longer even be available.
Sorry, I couldn't think how to explain that better. But I think the situation will be clearer with this KeyPress event procedure:
Private Sub txtUnitNoToSearch_KeyPress(KeyAscii As Integer)
Debug.Print "Text: '" & Me.txtUnitNoToSearch.Text & "'"
Debug.Print "Value: '" & Me.txtUnitNoToSearch.Value & "'"
End Sub
Watch the output from Debug.Print in the Immediate window as you make changes to the context of the text box. (Ctrl+g will open the Immediate window.)
One final point is that just Me.txtUnitNoToSearch gets you its default property which is .Value. Keep that in mind as you work through the rest of your code.

MS Access form to edit specific record from a form by providing input through text box

Can someone please help me on this find specific record Edit through MS access Forms
I have Frmfind form where I have one filed "ticket#" is a input text box
another filed was button "find"
When I enter ticket# which is primary key for my table. I need get the the specific ticket# record should be opened in FormEdit mode using VBA code...
So I have another form "frmEdit" of specific record which has to be called from frmfind -> specific input..
note: Ticket# is column in my table whcih it is primary to have the ticket#.
Code:
Option Compare Database
Private Sub find_Click()
If IsNull(Me.Text79) Or Me.Text79 = "" Then
MsgBox "You must enter a Ticket #", vbOKOnly, "Required Data"
Me.Text79.SetFocus
Exit Sub
End If
If [Ticket#] = Me.Text79.Value Then
MsgBox "Record found"
DoCmd.Close
DoCmd.OpenForm "frmEdit"
Else
MsgBox "not matching record"
Me.Text79.SetFocus
End If
End Sub
Private Sub Form_Open(cancel As Integer)
'On open set focus to text box
Me.Text79.SetFocus
End Sub
You can use this code:
Private Sub Command112_Click()
Me.txtSeach.SetFocus
On Error Resume Next
DoCmd.OpenForm "frmEdit", , , "TicketNumber = '" & Nz(Me.text79, "") & "'"
End Sub
Note in above if TicketNumber is in fact a number column and not text, then remove the single quotes and use this:
DoCmd.OpenForm "aa", , , "TicketNumber = " & Nz(Me.text79, "")
Then for your message, just place that code in the forms on-open event that has a cancel:
eg:
Private Sub Form_Open(Cancel As Integer)
If IsNull(Me!TicketNumberID) Then
MsgBox "Please enter a valid Ticket #", vbOKOnly, "Required Data"
Cancel = True
End If
End Sub
The above assumes your search column is ticket number.
You can also use dlookup(), or even dcount(). I think above is less code, but:
Dim strWhere As String
strWhere = "TicketNumber = " & Me.text79
If DCount("*", "tblBookings", strWhere) > 0 Then
code for Record found goes here
Else
code reocrd not found code here
End If
So either way should suffice here.