acNewRec doesn't work for combobox NotInList - ms-access

I have ComboBox in Access form. It show list of items from db table. If item isn't found I want to create this record. So I use NotInList event:
Private Sub Combo9_NotInList(NewData As String, Response As Integer)
DoCmd.GoToRecord , , acNewRec
End Sub
When I'm trying to enter not existing item in ComboBox, it gives error:
But I created button and added the same code onClick event and it's working without problem.
What is wrong with NotInList? How I can use this event to add new record?

You need to clear the error and whatever is entered.
Private Sub cboSelect_NotInList(NewData As String, Response As Integer)
Response = acDataErrContinue
MsgBox "New entries are not permitted." & vbCrLf & vbCrLf & _
"Please select an entry from the list or " & vbCrLf & _
"move to a new record and add a new item below. ", , _
"MsgBox Title"
Me.cboSelect.Undo
End Sub

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

Custom navigation fails upon opening an individual record in Microsoft Access project

I'm having issues with my Microsoft Access project.
The project comprises of two forms called, InfoForm and SearchBox.
InfoForm form
The InfoForm form is the primary-form, and has the following:
Custom navigation buttons, which are; Next and Prev
A custom Search button, which opens up the SearchBox form.
SearchBox form
The SearchBox form is used to browse and open individual records, and has the following:
List of available individual records
Open button
Individual records
The individual records open into the InfoForm form.
I use this code on the Open' button of theSearchBox` form, as follows:
Private Sub Command1_Click()
Dim strLN As String
strLN = Me.SearchResults.Column(0)
Dim strGN As String
strGN = Me.SearchResults.Column(1)
Dim strMN As String
strMN = Me.SearchResults.Column(2)
DoCmd.OpenForm "InfoForm", acNormal, , _
"[Last Name] = '" & strLN & "' And " & _
"[Given Name] = '" & strGN & "' And " & _
"[Middle Name] = '" & strMN & "'"
DoCmd.Close acForm, "SearchBox"
End Sub
This code works perfectly when the InfoForm form is first opened, right up until an individual record is opened.
At this point, the navigation buttons "Next" and "Prev" on the 'InfoForm' form stop working.
Please help. Thank you.
As #Andre noted, if the form is filtered to a single record then there are no succeeding or previous records to navigate to. Does your filter criteria result in a single record dataset?
An alternative is to open the form unfiltered (or with a filter that returns a restricted dataset but will usually still have multiple records) and 'go to' the desired record, then there will be succeeding and preceding records to navigate. Example from my code:
To open form:
DoCmd.OpenForm "Samples", , , , , acDialog, strLabNum
Then code behind the opened form:
Private Sub Form_Open(Cancel As Integer)
Me.RecordsetClone.FindFirst "LabNum='" & Me.OpenArgs & "'"
Me.Bookmark = Me.RecordsetClone.Bookmark
End Sub

VB sxript to make a questionaire form in MS Access 2010

Hello I just want to know on how to make a continuously questionnaire form in access that will update all the table once all the question have been filled. but before it reached end of the page, the user will be able to edited back and forth from forms to forms.
so it will be like this:
Total 30 forms
One form only update 1-3 fields in the table
Once the user finished with one form, the form will close and open another form.
The user will be able to back to previous form and edit it.
The buttons that it available only 2 buttons back arrow and next arrow(save data and move to another form and close current form)
The last form will save all the data.
When user finished all the question, the form will allows to be reopened from the first one and it will insert entirely new line of user data in table.
The one that i have done is
I create a form, with a box and tag, connected directly to the table, so it will updated in real time. so user can back and forth to edit it
"Next" button, using macro to close and open new form.
Final form using the folowing VB code to update the table:
Private Sub Close_Click()
CurrentDb.Execute "INSERT INTO Demographics(vid, cid, dobd, gend, heght, heght2, wgt, wgt2, lschool, secschl, qualify, hqlify, army, abranch )" & _
"VALUES('" & vid1 & "','" & cid1 & "','" & dobd1 & "','" & gend1 & "','" & heght1 & "','" & heght21 & "','" & wgt1 & "','" & wgt21 & "','" & lschool & "','" & secschl1 & "','" & qualify1 & "','" & hqilfy1 & "','" & army1 & "','" & abranch1 & "')"
cmdClear_Click
cmdClose_Click
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Private Sub cmdClear_Click()
vid1 = ""
cid1 = ""
dobd1 = ""
gend1 = ""
heght1 = ""
heght21 = ""
wgt1 = ""
wgt21 = ""
lschool1 = ""
secschl = ""
qualify = ""
hqlify = ""
army = ""
abranch = ""
End Sub
Private Sub Form_Current()
End Sub
Problem:
Final page script above wouldnt insert the data into the table at all.
It can insert data into table, if i indexed the column in the table, but it will end up messy if i did a lot of updates
My Questions:
Can anyone suggest me the correct VB script to do this continuously form activity, instead of update entire table per-form.
How do I create a form like in access 2003 on which i can create switchboard with login for user only, and special login access to the database only for administrator, so it will like an application. (im using Access 2010 .accdb file)
sorry for the long post, just want to make sure everything is clear, any answer would be greatly appreciated.
Thank you in advance
Regarding your first question:
This is one of those cases where a bound form could prove to be handy.
You can use INSERT INTO, but you'd need to generate a different query for each form in order to avoid errors. If you set all of your forms to bind to the same table, though, the form fields can interact directly with the table without requiring a lot of code.
Try the following example, using 3 forms (though you can easily extend this out to 30 forms).
Each form has:
A hidden field, ctlID (bound to the table's ID field)
Any number of other fields, bound to matching fields in the table
A 'Next' button, cmdNext (or in the case of the last form, cmdFinish)
You can use the WhereCondition argument of DoCmd.OpenForm to keep referring to the same record across multiple forms.
Here's the code:
' Form1 code:
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub cmdNext_Click()
Dim iID As Long
iID = Me.ctlID.Value
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "Form2", , , "ID = " & iID
End Sub
' Form2 code:
Private Sub cmdNext_Click()
Dim iID As Long
iID = Me.ctlID.Value
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "Form3", , , "ID = " & iID
End Sub
' Form3 code:
Private Sub cmdFinish_Click()
DoCmd.Close acForm, Me.Name
End Sub

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.

Possible to set filter on subform from parent form before subform data loads

I have frmParentForm with multiple controls used to build a filter for frmSubForm.
On frmParentForm_Load, I am doing (simplified example):
Me.sbfInvoice_List.Form.filter = "[created_on] >= #" & Me.RecentOrderDateCutoff & "#"
Me.sbfInvoice_List.Form.FilterOn = True
The problem is, on initial load, it seems the subform load is occurring first, so the entire table is loaded.
Is there a way (in a different event perhaps) to properly set the subform filter from the parent form so it is applied before the subform does its initial data load? (The subform can exist on its own, or as a child of many different parent forms (sometimes filtered, sometimes not), so I'd rather not put some complicated hack in the subform itself to accomplish this.)
Because the subform loads before the parent form, the parent form can not set a subform filter before the subform initially loads.
If you want to use the subform flexibly (all records when stand alone, but different subsets of records when included on different parent forms), I think you have to modify the subform to do it.
Private Sub Form_Open(Cancel As Integer)
Dim strParent As String
Dim strMsg As String
On Error GoTo ErrorHandler
strParent = Me.Parent.Name
Select Case strParent
Case "frmYourParentForm"
'set filter to only records from today '
Me.Filter = "[created_on] >= #" & Date() & "#"
Me.FilterOn = True
Case "frmSomeOtherParent"
'do something else '
End Select
ExitHere:
On Error GoTo 0
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2452
'The expression you entered has an invalid reference to '
'the Parent property. '
Resume Next
Case Else
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure Form_Open"
MsgBox strMsg
End Select
GoTo ExitHere
End Sub
Edit: If you want to track the sequence of events in the parent form and subform, you can add procedures like this one to the forms' modules.
Private Sub Form_Load()
Debug.Print Me.Name & ": Form_Load"
End Sub
Here is what I get when tracking the Open and Load events for my parent form and subform.
fsubChild: Form_Open
fsubChild: Form_Load
frmParent: Form_Open
frmParent: Form_Load