I have a Form in MS Access 2010. The form is bound to a record source (just a simple table). I am trying to use code like the following :
Change a field in the table (the current record) to another value (error is the field is not defined in VBA which causes it to not change in the table)
AFieldInTheTable = Value
Go to another record in the table (error is nothing happens at all)
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
Is there some option I am not setting because I tried building the form using the built in wizard and from a blank form but still no difference.
It is like Access doesn't know that the form is bound to the table. But I know that it is. So frustrating.
You cannot access / alter properties of a form using public functions which were called by other forms.
Only during events of the form can you perform actions with that form.
Related
I have a MS access data base which has a form from which I want to open a report. There is a choice of 26 reports which all have different name which is a numeric value eg 221.1, 221.2 113.3, etc etc. I have an unbound field in the form which adds the values of four other bound fields and returns a number which should match one of the report names. I have called this unbound field "report Name". I want to add a button that is pressed which opens a report that has the same name as the value in the "report name" field so that if the value is 221.1 it will open report named 221.1 and so on...
I am not used to coding in access and generally rely on the built in commands, macros, wizards etc but in this case I don't think there is a suitable "wizard" to use. Any ideas or help on how I can do this would be gratefully received. I am working in Access 2013 but the data base was written in 2007 and transferred over.
You can get away with just a tiny bit of code in your button's click handler:
Private Sub MyButton_Click()
DoCmd.OpenReport Me![report Name]
End Sub
Add the code just like adding an embedded macro, but by choosing Code Builder instead of Macro Builder. The first and last line should already be there.
I've been having this error where I am unable to set the record source for a subform.
Just a bit of background, the form in question is structured as such: frm1View is my unbound main form which contains 2 subforms, subfrm1Particulars and subfrm1Datasheet. The issue is with subfrm1Particulars, which itself contains 2 subforms in a tab control.
I am attempting to change the recordsource of my subform in subfrm1Particulars dynamically, based on the records found in the other subform (one sub form displays courses completed and the other displays courses scheduled; the idea is to remove scheduled courses they have already completed from that sub form view).
I am using the following code to do this dynamic recordsource change. The code is contained in subfrm1Particulars(which contains subfrmScheduledCourses)
strSQL = *blah blah blah where etc etc not like etc*
Me.subfrmScheduledCourses.Form.Recordsource= strSQL
I've checked numerous times, my subform control name is correct (although my subform shares the same name as the control, so maybe that's the issue). I have almost the exact same form configuration in another form, with the same code (i.e. the same recordsource and SQL statements and method of assigning recordsource) and it works perfectly, so I can't tell why this isn't working.
It gives me an error 3251. I'm running this in the On Enter event of the subform control (in both of my forms where this particular code appears).
Any help at all is much appreciated!!
Referring to subform objects can be tricky, especially from other subforms. Try calling the object differently:
Forms!frm1View!subfrm1Particulars.Form!subfrmScheduledCourses.Form.RecordSource
What this does it begins referring to form objects by navigating from mainform to subform to the desired area.
Background:
I am trying to add a new form to an old Access database that was created with Access 2003. Previous forms were based on tables, but the fields for the new form are based on a query.
I have a form called MasterList with subform MasterList_Sub. MasterList contains textboxes that I want to use to filter MasterList_Sub, along with a search and clear button. MasterList_Sub is displayed beneath.
I modified the VBA for the search button from the other forms, but it does not seem to work. Here is the code from a working form:
Private Sub SEARCH_Click()
Forms!mrtgref!Mrtgref_sub.Requery
End Sub
My code is simply
Private Sub SEARCH_Click()
Forms!MasterList!MasterList_Sub.Requery
End Sub
When I press the search button something appears to happen, but the subform does not update. I'm new to programming for Access, but based on what I've read this code looks too simple, like I'm missing something. I can't find any other VBA modules in the DB, and the fields are set up similarly to the other DBs. Any ideas on how to proceed from here?
Also, I've tried some other syntax, and when it doesn't work I get an error that starts with "Mortgagee Inquiry can't find..." Where is it getting the name Mortgagee Inquiry from?
MasterList_Sub is a subform control. .Requery is a method of the Form itself therefore you need to add .Form before .Requery.
The structure:
Forms!MainForm!SubformControl.Form.Requery
In your case:
Forms!MasterList!MasterList_Sub.Form.Requery
MS Access 2007, Win 7 32-bits
Is there a way where I can access an open query in datasheet view in access to get the current field value and current field name?
I won't put it into a form since it is a crosstab query and i'd have to generate and get rid of controls dynamically but i'm not fond of messing with forms controls that way with VBA. I know i can put a dynamic column report and bind an event on the controls but I'm asking if there are events or objects that can let me access directly the query.
Perhaps a recordset clone? But I haven't find anything on google.
Of course this is in VBA
Best regards,
It may be possible to work around your requirements. The crosstab is contained by a subform:
Source Object : Query.xtab
The Control Source for the two textboxes is:
Ctrl : =[screen].[activecontrol].[name]
Content: =[screen].[activecontrol]
Which means they show which ever column ans column contents the user selects in the crosstab subform. However, they will also show any other selected control on the form. ClickMe does not change the selected control, so the selected items remain the same in the textboxes.
You can also enter MacDermott's code for getting the current control's index, so the current control selected on the xtab query subform is displayed dynamically
Public Function ControlIndex(ctl as Control) as long
Dim i as Integer
For i=0 to Me.Controls.Count-1
if me.Controls(i) is ctl then
ControlIndex=i
exit for
end if
next
End Function
And finally this can help when changing from one control to another in the same record to keep the textboxes current.
I have made an Access 2007 db. I will be writing some basic vba for the appropriate event of a form so that a modal form is displayed when the original form is opened. This isn't difficult, but that form will have a drop down box of IDs from a particular table. The user will select an ID, but I want the selected ID from this form to go back to the parent form. E.g.:
Car form opened
Event is fired to open a modal form
IDs for a FK to cars has to be selected from a combobox (eg ID of driver - for simplicity, lets assume one car can have many drivers but not vice versa so 1:n only)
There is a button to confirm the selection. On clicking this button, the form closes, and the selected Driver ID is automatically inserted into a DriverID textbox on the car form (probably will be read only).
The last step I am not sure about. How can that be done in VBA?
This is one of the worst Access threads I've ever seen on StackOverflow.com, because every answer is wrong in at least some crucial aspect -- not a single one of them would actually run if you pasted them into VBA in an Access database.
The key principles here:
Open the dialog form modally (with the acDialog arguments)
Don't close it when the value has been confirmed, but instead set it's .Visible property to False.
Then in the calling form, read the value out of the hidden form, and then close it.
Something like this:
DoCmd.OpenForm "dlgPickDriver", , , , , acDialog
If IsLoaded("dlgPickDriver") Then
Me!DriverID = Forms!dlgPickDriver!cmbDriver
DoCmd.Close acForm, "dlgPickDriver"
End If
[IsLoaded is a Microsoft-provided function; I posted it here on StackOverflow recently, but would assume that the vast majority of Access developers writing VBA would have been using it forever]
I would recommend against having code that runs in the dialog form poke the data into the parent form because it's then impossible to use the dialog form in multiple locations. Having the dialog form know as little as possible about the context in which it's called is good programming practice. On the other hand, it's true the calling form needs to know the control name it's getting the value from, but that's a more sensible context than the other way around, in my opinion.
To gain 'access' (get it?!) to controls on separate running forms you can use the following:
Forms("AnOpenFormName").Controls("ControlName") = value
Edit: I used the wrong brackets AMG!
You could write a function that will display the form modally and then return the user selected value as a return value of it (similar to msgbox/inputbox).
EDIT: You could write a function such as following.
Function GetUserSelectedCarID() as string
dim myPopupForm as Form
set myPopupForm = new Form
myPopupForm.Show vbModal
GetUserSelectedCarID = myPopupForm.UserSelectedCar
End Function
UserSelectedCar is a property that stored the user selection on the popup form.
EDIT2: You could also add a property on popup form to see if the user clicked OK or Cancel.
It will return blank from the above function, if user clicked Cancel.
All of the above answers are good. I especially like the third way of wrapping this kind of functionlity into reusable function calls. The only thing I would add is do not CLOSE the modal form...otherwise you cannot get the selected value. Instead do me.visible=false.
So you do somethiing like this.
Public Function GetCArKey as Integer
dim intReturn as integer
docmd.openform "MyModalForm",,,,,,acdialog
'(the click event of the okay button of this form does me.visible=false...clicking Cancel will close the form.)
if isloaded("MyModalForm") then
intReturn=Forms("MyModalForm").Controls("ControlName").Value
end if
GetCarKey=intReturn
End Function
It would seem much easier to me to create a global variable in a module (eg Public gi_Value as Integer) and then set this in the child form. The value can then be read anywhere.