According to the Access (2003) documentation, when you delete a record, it's placed in a buffer, then the BeforeDeleteConfirm event is raised, then the AfterConfirmDelete ad Delete events. In my table if a record is deleted, I need to add one of its field values (a quantity value) to a field in another table.
I tried setting the quantity to a private variable in the Current event, but as soon as I delete click delete, it looks like the Current event fires again and resets my variable.
How can I access the value of a field in a deleted record?
The values in bound controls are still available at the form delete event. In this sample, txtid is a bound text box.
Private Sub Form_Delete(Cancel As Integer)
Dim strActivity As String
Dim strSql As String
strActivity = Me.txtid & " deleted."
strSql = "INSERT INTO tblAudit (activity) " & _
"VALUES (""" & strActivity & """);"
CurrentDb.Execute strSql, dbFailOnError
End Sub
This solution may require some redesigning, but it's what I use for a form of my own:
Add a boolean field to your record table called "Visible".
On your main form, set your form.filter="[Visible]=True"
On your main form, set AllowFilters=Yes.
On your main form, set your Form.Allow Deletions=False (we don't want them actually deleting the record)
Make a custom Delete command button that sets Visible=False.
If you feel the need later, you could add some maketable queries and delete queries to clean up your main table and move the deleted values off to another table.
This way, the user doesn't actually 'delete' the record. Rather, they just make that record invisible. And later, when the form is unloaded, you could add in some clean-up queries.
Generally, if I want to do something in the OnDelete events other than the default actions, I will tend to turn AllowDeletes OFF and create a DELETE RECORD command button that does what I want.
Thus, I don't depend on the built-in events. If I need to store a value, I do it, the pop up the confirm message, and then do what's needed, by whichever method I consider easiest.
I just find this a lot easier, particularly because user settings can cause things to behave differently. For example, the two Confirm events don't happen when the users has these settings (from the A2003 help file, but it's always been this way):
Note The AfterDelConfirm event does
not occur and the Delete Confirm
dialog box isn't displayed if you
clear the Record Changes check box
under Confirm on the Edit/Find tab of
the Options dialog box, available by
clicking Options on the Tools menu.
So I just avoid the whole thing by rolling my own deletion code and avoiding the default behaviors.
(and yes, if you've followed my posts over the years, you know that it's pretty unusual for me to advise against simply using default Access behaviors, but this is one case where I feel more control is appropriate in almost all cases)
Related
I have an Access 2010 DB that I need to update a field in. Right now I have a button on one of my forms that will duplicate the selected row. I know that I can make a field not duplicate by setting the Locked property to Yes on that field.
What I need to do is find a way to make a field not duplicate, but still be editable to certain people. I don't want to set the Locked property as that will lock it for everyone.
I've no idea how to do this except to change the working of the Duplicate record button. Right now it simply makes a copy of the row that is selected and pastes it into the New Record row. This takes all fields that are not Locked.
I read about possibly using a bunch of variables and setting them to the values of each of the fields, but this seems cumbersome since I have 160+ fields that I would have to setup. And then pasting them into the New Record row. I would prefer not to have to do this.
I have used VBA just not with Access very much. A VBA solution is fine with me, I have not tried anything as I don't know where to even start.
I suggest a sub to lock / unlock the controls, like this:
Private Sub SetControlsLocked(ForceLocked As Boolean)
Dim bLocked As Boolean
Dim sUser As String
If ForceLocked Then
' Always lock before copying
bLocked = True
Else
' More secure method than Environ("USERNAME")
sUser = CreateObject("WScript.Network").UserName
' Lock for everyone except some users
' If the list is longer or changes regularly, lookup the Username in a table instead
bLocked = Not (sUser = "jane" Or sUser = "bob" Or sUser = "mike")
End If
' do the locking
Me!Control1.Locked = bLocked
Me!Control2.Locked = bLocked
End Sub
(refer to comments on https://stackoverflow.com/a/32565953/3820271 for "more secure" claim).
and apply in your copy button routine...
Private Sub cmdCopy_Click()
' Lock before copying!
Call SetControlsLocked(True)
' ... your existing copy routine
' Unlock for special users
Call SetControlsLocked(False)
End Sub
... and when opening the form:
Private Sub Form_Load()
' Lock for non-special users
Call SetControlsLocked(False)
End Sub
What I do is have a table of users and their permissions level and Windows login username. I capture username with Environ("USERNAME") and match with the users record and set up forms appropriately, usually just disable/hide particular buttons.
But really have to do a lot more setup of the database to prevent users from accessing restricted data. Like hide navigation pane, disable ribbon, disable right click shortcut menu, disable function keys, maybe even publish as executable.
But if they know where the backend is, what stops them from just opening it? Unless you go to something like SQLServer database, really nothing.
I have an Access form with two subforms, both in continuous mode. Since I cannot have a subform inside a continunous form, I had to do it that way (Datasheet is not an option either)
Either way, when I click on my first subform, I change the other subform record source using some rather simple code :
Public Sub MAJFiltre(intIdMembership As Integer)
IdMembershipFiltre = intIdMembership
Me.RecordSource = "SELECT * FROM T_PeriodeMembershipPartipant WHERE IdPeriodeMembreship=" & IdMembershipFiltre
Me.Requery
End Sub
This function is called from the first subform. I did this for another form and it was working fine. For this one, if I use a breakpoint, I can see the recordsource is changed, but nothing happen in the UI. However, if I put a breakpoint in a BeforeInsert event, I can clearly see the recordssource reverting to the original one (with no WHERE clause)
I also notice something unusual : If I save the form code while debugging, all of a sudden, it works. However, as soon as I close the form, it revert back to its "buggy" version.
Does anyway have an idea of what is going on and how I can correct /prevent it ?
Thanks
It sounds similar to a problem we suffer periodically, which relates to subforms linked to a parent form with link master/child ids: If you've (ie Access has done it for you) unintentionally saved a filter value as a property in one of the subforms, (filter by or in a record source), when your code reassigns the record source, this saved value prevents the new record source from being assigned correctly, and ms access then does what it thinks is best - in this case, reverting to the valid (prior) record source.
Check your subforms for unwanted saved values of data-tab properties.
Could the problem be that your field name is misspelled in your query criterion?
IdPeriodeMembreship
If the field in your T_PeriodeMembershipPartipant table is IdPeriodeMembERship instead of IdPeriodeMembREship your query would likely prompt you to enter the parameter value manually when it is run.
If that field does exist in your table but the value you're specifying in your criterion isn't found in that field, it will return no results and the second subform's recordsource will be set to an empty recordset.
I am completely new to VBA. I have been told to add records to a table by using a form and a Save button and given some very basic instructions. While I have achieved this with inline query, I have been told to follow some strict methods like usage of QueryDef/QueryDefs and .Parameters.
So far I am trying a very basic project, just to grasp the concepts, but I am unable to add any record to an empty table. In case the table is not empty(I manually enter a record), whenever I click the Save button for saving newer records, the number of records added are somehow doubling with each instance. For example, when I Save for the 1st time, 1 record is added, 2nd time 2 records of the same type is added, 3rd time 4 are added and so on.
The table(tbl_test) has 2 fields --> ID(primary key), Source(Long Text) and Reg No (Number).
The query(qry_test) is made with the Append feature and I have been told to add expressions which makes the code like this -
INSERT INTO tbl_test ( Source, [Reg No] )
SELECT [strSource] AS Expr1, [lngRegNo] AS Expr2
FROM tbl_test;
The form has 2 fields for Source(txt_Source) and Reg No(txt_RegNo) which have blank Record Sources (Unbound). The Save button has the following Event Procedure -
Private Sub btn_save_Click()
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("qry_test")
qdf.Parameters("strSource") = Me.txt_Source
qdf.Parameters("lngRegNo") = Me.txt_RegNo
qdf.Execute
End Sub
I have zero knowledge about VBA and would gladly accept ANY help. It would be great if I get any sort of source code that will explain to all the details about saving records from forms and editing them using these querydef, parameter and recordset stuff.
Welcome to StackOverflow!
If you are using a form to collect data for records that are stored in a table, running a saved append query by QueryDefs to do this is, in my opinion, not the best method.
Whilst an append query does add new records to an existing table, I would tend to use an append query where I've got multiple established records that I want to add to an existing table.
If I'm setting up a data entry form that is designed to collect new data for new records 1-at-a-time, an append query doesn't really suit this purpose.
Instead, there are a number of features built in to the design of MS-Access forms to help collect data and save it to a table. This is because forms are very much intended to be set up so that users can interact with records from a table in a controlled, user-friendly way rather than interact directly with the table object itself.
The first and most important feature of a form in this context is probably the form's record source property. When you create a new form, go in to design view for the form and open the form's property sheet (F4 key). In the "Data" tab of the form's property sheet you'll find the record source property:
The record source essentially connects your form with a set of records, whether those be records in a table object, a query object or an sql query string.
In your case it would be better, in my opinion, to bind your tbl_Test table to your form by referring to it in your form's record source:
It will seem like nothing has happened to your form, but if you now open the "Add Existing Fields" panel (alt + F8), you'll notice that the fields associated with your tbl_Test table are available to you:
Drag them to the detail section of your form...
Then put your form in to Form View:
Essentially what you and your users are seeing is the first blank record in your tbl_Test, but displayed on a form instead.
Entering data in these fields on the form...
...will put that data in to the table we specified in the form's record source...
So hopefully you can see that setting the form's record source property to that of your table, is much cleaner than trying to get an append query to collect data from your form and deliver it your table.
At this point you're probably asking a few questions:
When I have filled in the fields for a record on my form, how do I save that record?
More can be said about this, but for brevity, I'd recommend using a command button for running some vba to save the record; similar to what you've done, but utilising the form's Dirty property instead using an append query:
Here's the click event VBA for my save button example:
Private Sub cmdSave_Click()
If Me.Dirty Then
Me.Dirty = False
End If
End Sub
Me.Dirty is a Boolean (True or False) setting for the form; essentially it is automatically set to True when a user changes something on the form. To save those changes, the Me.Dirty setting will have to be set to False.
Me.Dirty is a bit like the swing gate on a sheep pen. When a shepherd puts a sheep (data) in the pen (form) they will open the gate to the pen. The open gate is like the form's Me.Dirty being set to True. To lock the sheep (data) in, the gate needs to be closed, or in the case of forms, the Me.Dirty property needs to be set to False. The VBA above essentially checks to see if the gate was opened and if it was to close it.
How do I move to a new record in the form once I have saved the current one?
Again, I'd give the user a command button to do this and run some VBA on its click event:
Here's the VBA for moving to a new record:
Private Sub cmdNew_Click()
DoCmd.GoToRecord , , acNewRec
End Sub
Summary
There is a lot more to consider than what I've outlined here, such as:
validating data before it is saved
checking other form events (such as close) to ensure data entry is not lost
navigating to an existing record
But hopefully what I've given you here is at least pointing you in a better direction. Best of luck!
Access is optimized for bound forms. Unbound forms are for special cases only.
Should you prefer unbound forms (could be for many reasons), Access isn't worth the trouble, and you should turn to Visual Studio and WinForms.
In either case, browse for a beginner's tutorial.
If you want to use an unbound for or unbound controls you can create the SQL string in the procedure:
Dim mysql as String
mysql = "INSERT INTO tbl_test ( [source], [reg_No]) VALUES (Me.txt_Source, Me.txt_RegNo)
DoCmd.RunSQL mysql
In Access Properties for a subform, I have "LinkChildFields = '' " and ""LinkMasterFields = '' " (i.e. blank for both, this is what I want: no link)
When I change the Recordsource of the subform, and requery the subform, (in VBA) they are both automatically being set to a field, in my case "CaseID" (resulting in no records being displayed)
This is my recordsource:
stSQL = "SELECT qryCasesAndCards1.CaseID, qryCasesAndCards1.CaseStatusID, qryCasesAndCards1.Status, qryCasesAndCards1.CompanyName, qryCasesAndCards1.NumberOfCards, qryCasesAndCards1.FullName, qryCasesAndCards1.CaseCreatedDate, qryCasesAndCards1.CaseClosedDate, qryCasesAndCards1.CreatedBy" & _
" FROM qryCasesAndCards1 where not caseID = " & Me.CaseID & " and cardnumber in (select qryCasesAndCards1.cardnumber from qrycasesandcards1 where qryCasesAndCards1.caseID = " & Me.CaseID & ")"
I've tried substituting a simpler query "select * from qryCases1" and the problem still occurs.
In VBA, right after the requery, I am debug.print'ing ".linkchildfields" and ".linkmasterfields" and this is how I can see they are both being automatically set to "CaseID"
I have tried changing both values in Access form properties to nonsense value, resaving, changing again, resaving etc, and still no joy.
I can workaround problem by setting both those values right after the recordsource runs, but there is an unacceptable delay when doing this (about 5 seconds for each value)
One thing I'm just wondering now, is wether my form filter is being propagated to the subform? (am opening it via "docmd.open ... caseid = x" )
CaseID might have at ONE stage some long time ago been entered in to those link fields... but it's definitely not now. It's like they're locked in an Access vault somewhere and it's thinking "golly gee i'm pretty sure he wants CaseID in there gee whiz I'll get right on that!"
I've found MSAccess: Change in subform recordsource causes loss of LinkChildFields binding and I've found Linking SubReports Without LinkChild/LinkMaster but I can't get any help from them.
Thanks for any help offered: I'm tearing my hair out :)
To keep Access from automatically updating the LinkChildFields and LinkMasterFields on a subform...
Ensure that the RecordSource queries do not have matching key fieldnames.
Ensure that the Link Child Fields and Link Master Fields are blank on the SubForm object's Property Sheet.
DO NOT set the LinkChildFields and LinkMasterFields properties in code (i.e. remove any previous code like MySubForm.LinkChildFields = ""), otherwise the negative side effects may not be resolved.
If the form and/or subform are bound directly to tables, simply update one of them to query the table and add an alias for one of the key fields. For existing queries, it is possible to add an alias for one of the key field names without negative consequences in probably most cases. In my case, I only had to add "AS Acct" to a single field in the SELECT part of the subform's RecordSource SQL--no other change was necessary in the FROM, WHERE or ORDER BY clauses even though the original [Account] fieldname is referenced in each of those clauses.
SELECT Actions.[Account] As Acct, ...
Of course, this required an update to the ControlSource property for the relevant control on the form.
These changes eliminated the extra few-second lag when changing both the parent record and when switching records on the subform! Likewise, it eliminated redundant events during record navigation!
I found no other setting that keeps Access from automatically re-binding the subform via the LinkChildFields and LinkMasterFields properties. As already pointed out in the comments, it also doesn't help by resetting the link fields (e.g. LinkChildFields = "") after setting RecordSource, primarily because Access takes time to reset the fields, much of that time spent in events fired both upon the automatic assignment of the link fields and again when they are reset to blank. (e.g., Form_Current is called at least twice for each new record navigation.)
Years passed... as even on Microsoft Access 2019, changing subform .RecordSource always alters original .LinkChildFields and .LinkMasterFields, reassigning good fields will cost us 5s time for each property, we have no way to avoid this.
As a workarround, we can use always the same query, for example, qrySelTmp, as .RecordSource for the subform, at the design time
Mainform.subform.Form.RecordSource = "qrySelTmp"
When we must change .RecordSource content, we change only the definition of the Query, like this
CurrentDb.QueryDefs("qrySelTmp").SQL = "SELECT qryCasesAndCards1.CaseID, qryCasesAndCards1.CaseStatusID, qryCasesAndCards1.Status, qryCasesAndCards1.CompanyName, qryCasesAndCards1.NumberOfCards, qryCasesAndCards1.FullName, qryCasesAndCards1.CaseCreatedDate, qryCasesAndCards1.CaseClosedDate, qryCasesAndCards1.CreatedBy" & _
" FROM qryCasesAndCards1 where not caseID = " & Me.CaseID & " and cardnumber in (select qryCasesAndCards1.cardnumber from qrycasesandcards1 where qryCasesAndCards1.caseID = " & Me.CaseID & ")"
If(Mainform.subform.Form.RecordSource = "qrySelTmp") then
Mainform.subform.Form.Requery
else
Mainform.subform.Form.RecordSource = "qrySelTmp"
End if
As shown in the code, we do only a subform .Requery instead of assigning a new .RecordSource.
As Subform.Form.Requery does not alter subform properties .LinkChildFields and .LinkMasterFields, we gain about 8s for each real change of .RecordSource.
In my case with 1 Customer - Many Orders diagram, I've a gain of time at the proportion of 2s over 20s.
What I done in the past is simply REMOVE the link child/master settings.
Then in code I SET the child critera. So in the following, the link master/child SHOULD filter by tour_id, but I simply INCLUDED the criteria in the SQL and then HAMMER OUT the settings.
Dim strSql As String
' load sub-form to this tour....
strSql = "select * from qryGuidesForTour2 where Tour_id = " & Me!ID
Me.subGuides.Form.RecordSource = strSql
Me.subGuides.LinkChildFields = ""
Me.subGuides.LinkMasterFields = ""
The above was a fix and fixed any performance issues. I suppose it really depends on "how" the main form record is set, but if you are setting up the "main" form record via code, then as above simply stuff in the child forms recordset directly with the critera you need. And while in above I did blank out the master/child settings, if you ARE going to allow adding of reocrds in the sub form, then likely in above you should set the master/child fields as opposed blanking them out.
This seem so simple but I can't figure it out. It seems like I'd want to set the Locked property to Yes, but that actually prevents them from selecting anything in the drop down!
When I say readonly, I mean having the combo box preloaded with values and forcing the user to only select one of those values without being able to type in their own.
Any ideas?
The standard way to achieve this is to set the Limit To List property to True.
The user will be able to type in invalid text but if he tabs away or presses Enter a popup will say
The text you entered isn't an item in
the list
And he will be forced back to choose something from the list.
It's not great, compared to the true dropdown list control you get elsewhere (VB6, Winforms, .NET, etc)
There is a Limit To List property you should set to Yes
There are two parts to what you've requested:
a. I mean having the combo box preloaded with values...
For this, there's nothing you need to do -- just set the rowsource appropriately. To add values to the list, you'd have to do some work, but in order to prevent, you need do nothing at all.
b. and forcing the user to only select one of those values without being able
to type in their own.
As explained in other answers, you need to set the LimitToList property to TRUE.
Now, the error messages you'll get from this are not all that great, so you'll likely want to define the NotInList event to be a little more friendly to the user. Something like this could be appropriate:
Private Sub MyCombo_NotInList(NewData As String, Response As Integer)
MsgBox Chr(34) & NewData & Chr(34) & _
" is not one of the choices in the list.", _
vbInformation, "Select from the list"
Me!MyCombo.Undo
Response = acDataErrorContinue
End Sub
If you wanted to add to the list, this is where you'd handle that, too, but since you don't, you need only concern yourself with informing the user of the problem (i.e., protecting them from the cryptic default error message).
you can also add this code into presskey event of combobox
KeyAscil = 0
msgbox " please select from list"
this will prevent users from write into combobox and show message error