Refresh specific line in a datasheet view in Access - ms-access

I have a form in "Datasheet" mode in Access 2003 which can contains 25 000 lines.
When I want to edit a specific line, I open a specific form which contains all the editable field.
Once finished, I save and close the form but now I'd like to requery and refresh only the line I edited. (I want to keep the cursor at this line, and I'd like to avoid reloading every line so a requery on the form isn't the solution).
Is it possible ?
Regards

No, I don't think it is. You would have to requery your form, but you can put your cursor back where it was by saving the AbsolutePosition before you do so:
lngPos = Me.Recordset.AbsolutePosition
Me.Requery
Me.Recordset.AbsolutePosition = lngPos
By the way, I stole this idea from Albert D. Kallal on pcreview. I have used the Bookmark object in the past, but it always seemed buggy.

Related

How to partially clear userform in ms access

I have a bound userform with lot of fields in it, I use submit button to let user make entries in shared form. But whenever user clicks on submit button it requery whole form. However, there are around 4-5 fields which should not be cleared on submit button, they should retain the value and rest of the fields should get cleared on every submit button click.
Can this be done in access and how ? below is the simple code is use to submit and requery the record.
Private Sub SaveBtn_Click()
RunCommand acCmdSaveRecord
Me.Requery
Me.DataForm.Requery
End Sub
When you have a "Bound Form" in Access, this means that the Access Form is tied to the underlying data source, which is specified by, and stored in, the "Record Source" property for the form. In the "SaveBtn_Click()" method you provided above, this code does the following things:
RunCommand acCmdSaveRecord - Saves the current record in the form.
Me.Requery - Requeries the entire form.
Me.DataForm.Requery - Requeries the subform named "DataForm" on your main form.
So when you "Requery" the entire form (depending on how the "Record Source" and other form property settings are setup), the requery operation in some cases moves the cursor to the new record in the data source (...its the default settings for the drag and drop form designer tools in later versions of Access), and I suspect that is why you see the form being "cleared" when you call "Requery." A couple of items I would point out, just for clarity:
Note 1: "Requery" actually saves the record, so explicitly calling "RunCommand acCmdSaveRecord" should not be necessary if you're going to call "Requery" too.
Note 2: Depending on how the Record Source (and other) form properties are set, Requery in some cases just saves and refreshes the currently selected record, but it doesn't sound like that is how your form is working (based on what you said above), so I'm assuming that is not the case in your form.
Note 3: The subform will also be requeried when you call "Requery" for the main form, so that line of code may also be redundant here. The reason to call "Me.DataForm.Requery" is if you only want to requery the subform and NOT requery the entire main form.
Note 4: The "DataForm" subform (on your main form) will have a separate data source for it's own "Record Source" property, which is separate from the parent (main) form, so it is important to be aware of that fact, depending on which field values you want to save.
So, that said, there are a couple of options I might suggest, depending on exactly how you want your form to behave:
If you want to keep some of the field values and use those for the NEW RECORD when you hit the requery button, you could always save them off in a variable and then set those controls again after requerying from your variables. Just create a seperate variable for each value you want to save, copy the TextBox control values into each of variables respectively, call requery on the form, and then copy those values back into your TextBox controls after you requery. That code would be something like the following (depending on the exact names of your TextBox controls, I used the fictitious name "txtMyTextBox" for this example):
Private Sub SaveBtn_Click()
Dim vValue1 as Variant
vValue1 = txtMyTextBox
Me.Requery
txtMyTextBox = vValue1
End Sub
Or, if you're just trying to create "Default Values" for certain controls, TextBox controls have a "DefaultValue" property you can set to any value you would like to use for the default. Those can be set from the Property Sheet on the right side of the Access window when the form is opened in Design mode or Layout mode and the TextBox control is selected. (Press F4 to open the Property Sheet if it's not already open).
But, if you really want to Requery and then go back to the same record you were previously on, you could try the following code:
Private Sub SaveBtn_Click()
Dim iRecord as Integer
iRecord = Me.CurrentRecord
Me.Requery
DoCmd.GoToRecord , , acGoTo, iRecord
End Sub
Anyway, I hope this all makes sense and you find it helpful. Please let me know if you have any further questions about this answer.

MS Access - Modify data in Form and save record

I have a simple question but somehow I just couldn't find a proper solution online.
In a form there is a text box. It gets its data via dlookup from a table.
With a button I want to allow editing by setting
Me!AllowEdits = True
So save there is a button with the code
DoCmd.RunCommand acCmdSaveRecord
Anyway - by clicking the editing button I'm not able to modify the text written in the text box.
Why are you using DLookup()?
The acCmdSaveRecord command will not save calculated data. This value returned by DLookup() will not be saved to table without first running code to save it into a field, like Me!fieldname = Me.textboxname. So you can have another textbox BOUND to that field and that textbox can be editable.

MS Access - Data Entered in a Form Automatically saves when i close the form

I've been tasked with making updates to an MS Access Database and its forms.
Each form seems to be linked to a query. If I enter data into a text box on the form and then close the form without pressing the Save Record button a new record is still added to the Database which makes no sense to.
Any insight would be great, I'm a programmer but have little experience working with Access Forms and Databases.
Thanks.
Microsoft Access binds forms to data by default, and will automatically save data as soon as you either move between records or close the form you're working on. For the average user, this is actually a good thing because it makes it very difficult to lose data, even if you accidentally close the form after making an edit.
If this functionality isn't what you're looking for, I'd suggest removing the binding from the form, that is, set the Record Source property to blank, then manipulate all the data in code using unbound controls. It's a lot more fiddly, but it gives you a lot more control.
The other option would be to use the form's BeforeUpdate event to ask the user if they want to save their changes before allowing them to go through.
If your main concern is accidentally adding new records, set the Allow Additions property to false, and create new records programmatically.
A sample of a BeforeUpdate event procedure (the same code would work for the BeforeInsert event):
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim intAns As Integer
intAns = MsgBox("Are you sure you want to save this record?", vbQuestion + vbYesNo, "Save Record")
If intAns = vbNo Then Cancel = 1
End Sub
If you were to use the above method, and the user clicked "No", they would have to then click the ESC key to undo the changes they had entered. Otherwise it would keep showing the dialogue when they moved off the record.

Access SubForm recordsource revert to original

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.

MS Access how can a subform get the value from another subform?

I have a main form that contains two other child subforms. When a textbox value changes on subform (A) I would like to set the value of a textbox on subform (B) equal to the changed textbox on subform (A).
So being completely new to Access I did some googling and it looks like this can accomplish this with some VBA. So on subform (A)'s textbox 'after update' event I have added this VBA.
Private Sub ID_AfterUpdate()
Me.Parent.Forms.formA.textboxDestination.Value = Me.textboxSource.Value
End Sub
I am doing this using the code builder. Then I save and run the form. Nothing happens. No errors.. nothing I am not even sure that piece of code is running. I'm not even sure how one debugs VBA in Access. Can someone help me out?
Thanks!
The after update event for a control fires when the user changes its value. If ID is bound to an autonumber field, the db engine will supply its value when you add a new record. However, since the user didn't make that change, the after update event does not fire.
For general debugging purposes, you can add a Debug.Print or a MsgBox statement.
Debug.Print "my event fired"
MsgBox "my event fired"
View the output from Debug.Print in the Immediate Window of the VB Editor. You can go there from your main Access window with the Ctrl+g keyboard shortcut.
Another technique is to set a break point on a line of your code. Right-click on the code line, then choose Toggle->Breakpoint from the context menu. Or click in the left margin so that a reddish dot appears to mark the breakpoint. Or press the F9 key. Or choose Debug->Toggle Breakpoint from the VB Editor's main menu.
You can also type Stop on a line by itself to trigger break mode. However, you would need to remove it later. Those other breakpoints I mentioned are temporary and don't get saved in the code module.
However you get to break mode, you can then step through the code one line at a time with the F8 key. That will show you which lines are executed. You could also inspect the value of a variable at any time in break mode by typing a question mark, then variable name, and then the Enter key in the Immediate Window:
? MyVariable
Explore the VB Editor's main menu to find additional debugging options. For example, the Watch Window will allow you to monitor the values of selected variables as you step through the code.
You don't have to specify the name of the parent form.
Me.Parent.CONTROLNAME.Value is a kind of working code.
Regards
On the update property of the textbox you are changing, put the following:
[Forms]![frmContainingValueYouWantToChange]![txtTextboxYouWantToChange] = "newValueHere"
Let us know how you get on?
I know it was 6 years ago, but I think the error here was that you were updating value from the formA to the same formA.
Me.Parent.Forms.formA actually igual to Me because you were calling :
subform (A)'s textbox 'after update' event
It should be formB and the word "Form" should be placed after (and the word "Value" isn't mandatory):
Private Sub textboxSource_AfterUpdate()
Me.Parent.formB.Form.textboxDestination.Value = Me.textboxSource.Value
End Sub
You can learn here some more informations how to Refer to Form and Subform properties and controls
Regards