Subform losing focus when Adding new record - ms-access

My DB is split into Front and Back. I have three tables that I am working with in this problem; tblMain, tblDetail, tblNote. I know that the number of records is relatively small but I am designing this for long term use. I have a bound form (frmMain) for tblMain that has a subform (subDetail) for tblDetail. SubDetail has a subform (subNote) for tblNote. When I click a button on the main form, frmMain, the subDetail form becomes visible and allows me to add a new record. I copied the button and the VBA to subDetail so I could do the same thing for adding a record to subNote if needed. However, after the record is created, the subNote form disappears and focus is returned to subDetail. If I move to a new record then back, the subNote form becomes visible and shows an empty record except for the PK. I've checked all the properties of both subDetail and subNote. They are the exact same except for the OnCurrent event for subDetail. I have the following for the subDetail OnCurrent Event.
'Hide subform if no records
Private Sub Form_Current()
Me!ctrlmlsID.SetFocus
With Me!subNote.Form
.Visible = (.RecordsetClone.RecordCount > 0)
End With
End Sub
This hides or displays the subform, subNote, depending on if there is any records or not. Any help in figuring out this behavior would be appreciated.
Here is the code for adding a new record to subNote upon button click.
If Me.Dirty Then Me.Dirty = False
Dim AddTransID As String
AddTransID = Me!ctrlID.Value
Me!subNote.Visible = True
Me!subNote.SetFocus
DoCmd.GoToRecord , , acNewRec
Me!subNote.Form!ctrlID = AddTransID
Me!subNote.Form!ctrlType.SetFocus
I've used breakpoints and the adding record works just fine. It's just the subform going invisible again and the focus being changed back to the parent that is the issue because it won't allow those notes to be added.

I get the impression you are fighting Access here.
You normally do not need a button to create a new record using a properly bound subform with AllowAdditions = True. That will happen automatically.
The problem you are running into may stem from the fact that the new records don't really exist yet until saved (as you observe there is no PK value). So the logic to hide subforms isn't going to work. If you insist on staying with this approach you might try
.Visible = (.RecordsetClone.RecordCount > 0 or .NewRecord)
But I think your life will be easier if you remove the buttons and code you have mentioned and let Access do what it is good at doing. Make sure your subform data binding properties are set right.

Related

How to keep focus after requery in MS Access?

I am using the following code in VBA to keep focus on a certain recordset even after requery :
With Forms!frmMain!frmMainSub
strControlName = .Form.ActiveControl.Name
lngCurrentPos = .Form.Recordset.AbsolutePosition
.Requery
.Form.Recordset.AbsolutePosition = lngCurrentPos
.Form.Controls(strControlName).SetFocus
End With
The problem with this code is that the subform is in datasheet view and is usually sorted by a field. Since the code above records the absolute position on dynaset- or snapshot-type Recordset object, it cannot keep track of the sorted datasheet.
Is there a better way to keep focus on a specific field after the form is requeired?
Edit:
This question is not a duplicate because there are answers about keeping the focus from the same form. The problem is that I am in a different form and also I want to preserve focus even when filter is applied to one of the fields.

if checkbox is marked, then create msgbox with form value in access

So I have an access form based on a table. The form has a list of colors and a yes/no checkbox next to it. If the user marks the checkboxes then clicks a button, i want a msgbox to appear to show all the colors next to the marked checkboxes. Here is the code I currently have, it does not run if I click the button a second time. It also sometimes only shows the first color and is buggy in general.
Form looks like this
Red x
Blue
Green x
Yellow x
Code looks like this
private sub command5_click()
dim rs as dao.recordset
set rs=me.recordsetclone
rs.movefirst
do while not rs.eof
if rs!checkboxes = true then
msgbox rs!color
end if
rs.movenext
loop
set rs=nothing
end sub
You need to add Me.Dirty = False at the top of command5_Click. Requerying the recordset is overkill - you lose your scroll position and your current record.
When you have a continuous form, data is written out to the database when focus is moved from the current record to a different record. This does not happen when you move focus to a control in the header or footer. This is by design, as it is a useful feature. Here's why:
Suppose you had a 3rd field in your database, a text field called "essay" where you could write a 10-line essay all about the color. The field is too big to show on the continuous part of the form, so you add a bound textbox to the form footer. As you move up and down through the color records, the essay for the current color will show at the bottom of the form. And it will be editable. When you click on the Essay textbox, the current record is still being edited. The current record can have bound controls in the header, detail and footer, and edits in any of those places will all be written to the DB simultaneously.
When you move focus to an unbound control (such as command5), it's no different. The current record is still the current record, even though none of its bound controls currently have the focus.
Whenever you want the current record to remain the current record, but to force its edits to be written to the DB, you use Me.Dirty = False.
As to why command5 only works the first time you click it? I have no idea!

MS Access de-select listbox after running macro/query

So I have a form (frmBookingForm) in Access, and a listbox (lstMyBookings) that displays the results of a query.
Below this, I have a button (cmdDeleteBooking) which runs a delete query using the lstMyBookings selection as input. The button then runs a macro that firstly checks whether a record in the listbox is selected, and if one is, runs the delete query and requeries the data, so the deleted record is no longer shown in the listbox. If not, an error message is displayed.
However, if I then click the button again, it again runs the delete query, even though there is apparently nothing selected in the list box.
Essentially, how can I 'clear' the listbox selection?
I'd prefer a solution that can be done in a macro format, as I have little to no understanding of VBA. However, if providing a VBA solution, I'd greatly appreciate a short explanation of it.
Thanks :)
Looks like this website has a nice little function to do it. Essentially, you need to test if it's a multiselect, and then do one of two things. I suppose if you know ahead of time that it is/isn't a multiselect, you wouldn't even need the "if" statement:
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
If the control's MultiSelect property is set to None, this code just sets List0 to Null. If the control's MultiSelect property is set to anything else, the code loops through all items that are currently selected, and sets the Selected property of that item to False. My example assumes your control is called List0.
EDIT
To use this code, use an event instead of a macro. Here's how you do this:
Right click on the button, select properties
In the Property Sheet window, click on the "Event" tab
Click inside of the "On Click" blank area, and click the dropdown arrow, then select "[Event Procedure]"
Click the ellipsis ("...") to go into the code editor.
In the code editor, your should already have your event for the button (let's assume the button is called Command1:
Private Sub Command1_Click()
End Sub
Add your code in between (assuming the listbox is called List0):
Private Sub Command1_Click()
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
End Sub

MSAccess 2010 - Subform in a subform not showing fields

I have a database with a main form, and on the main form is a subform. The subform also has a subform, which gets hidden unless a field on the first subform (the parent subform) is set to "ticket," at which time the second (child) subform appears. I got it working beautifully, except that the child subform appears blank, with no fields or labels on it. Just a background color that I set on the child subform itself.
I am using the following code in the AfterUpdate
Private Sub EventTypedd_AfterUpdate()
'Make the Ticket subform visible once EventTypedd is set to TICKET
If Me.EventTypedd.Value = "Ticket" Then
SBFCreateTicket.Visible = "True"
Else
SBFCreateTicket.Visible = "False"
End If
End Sub
Can anybody suggest possible reasons why the subform won't show the fields? The form itself shows up as it should, and the background color of the form shows up, but none of the fields or the field labels show up, and I can't figure out why. Anyone know why, or have a suggestion? Thanks!
I would like to ask a question or two but don't have the Stackoverflow kudos to do this. The issue maybe that you are not refreshing the final subform. So that when you go into the main form record without touching it, the final subform query returns zero records.
You then set flags on the top form and you should see results but the subform may not have re queried.
Try...
SBFCreateTicket.requery '<<< New line before your original code.
If Me.EventTypedd.Value = "Ticket" Then
SBFCreateTicket.Visible = "True"
Else
SBFCreateTicket.Visible = "False"
End If
Okay, I figured it out. Apparently, the AllowEdits on the second subform was set to NO, and I'm not sure why. As soon as I set it to YES, the form shows up and everything works perfectly. And as usual, as soon as I solve one problem, another one crops up a little farther down the workflow process, but that's a subject for another thread. Thanks to the ONE person that tried to help me out, anyway. :)

How to put a label as link at the end of coloumns in a subform (Datasheet) in Access 2000?

I have a database created in access 2000. A form with a subform (datasheet - defaultview) in it. I have added a label at end of the coloumns in subform and given hyperlink to open the object present in database itself. But when the form open nothing is visible after the coloumn ? I got four coloumns and had hide two columns via onload event of subform. the code is below
Me.SubGroupname.ColumnHidden = True
Me.GroupName.ColumnHidden = True
Me.BNFno.ColumnHidden = False
Me.BNFno.ColumnWidth = -2
Me.SubGroupName1.ColumnWidth = -2
How can I make it visible so that it will appear as a link at each row ?
Please help me.
If I understand you properly, you cannot use Datasheet view to display a control at the end of a row. Switch to Continuous Forms view, it will give you more control, but you will have to work a little harder to get a nice layout.
Alternatively, add a click event to one of the existing columns.