I have the following code
command_Click()
rDate = InputBox("MM/YYYY")
end sub
When I click the command the input box appears and I input my value. But then I get another pop up asking for the parameter value for rDate.
Any thoughts?
I assume you have a query that's like
Select *
From SomeTable
Where acolumn = rDate
The rDate defined in your command_click() sub is not the same thing. That rDate is a local variable which has nothing to do with your query.
You likely want to pass your parameter to a query. Let's say your query is called vikesQuery. Your command button click code would look like this
Dim qdef As DAO.QueryDef
Set qdef = CurrentDb.QueryDefs("vikesQuery")
If qdef Is Nothing Then
Exit Sub
End If
qdef.Parameters("rDate") = InputBox("MM/YYYY") 'though there are much better ways of getting your parameter like a textbox on a form
Dim rs As DAO.Recordset
Set rs = qdef.OpenRecordset
Now your query is in a recordset where you can do lots with it. Typically you would show this on a form.
Dim frm As Form
DoCmd.OpenForm "vikesForm", acNormal
Set frm = Forms("vikesForm")
Set frm.Recordset = rs 'sets the recordset of the form to the recordset you generated from your query.
This last step assumes you have a form set up to accept the structure of this recordset. i.e. textboxes bound to the right columns.
Related
How can I iterate a record set that returns a field of type field2?
Is there a way to tell how many objects are in the field2 type?
Let me describe the relevant aspects of my table:
The table fields has field NMR which contains a list of possible options a user can select in another table. In the Experiments table, the field NMR is a combobox with populates the options from the other table.
The way I do this is in the Experiments table design, I have set the field this way:
Now in one of my forms, I need to read the value in Experiments!NMR which can be a multiple selections allowed combobox. The recordset rs!NMR is of type Field2.
To get the values, you iterate using an integer (i.e. rs!NMR(0) would return the first selected option). The problem is I don't know how to get the field count and calling !NMR(i) where i is greater than the number of elements will invoke a Run time error '3265', Object doesn't exist in this collection.
Their exist a size method only returns the field width size (4?) and the documentation states this is the size of the data type within the field2 object.
There doesn't seem to be a count method associated with field2 as using !NMR.Count invokes runtime error 438, Object doesn't support this method.
Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qry As String
qry = "SELECT * FROM Experiments"
Set db = CurrentDb
Set rs = db.OpenRecordset(qry, dbOpenSnapshot)
With rs
Do While Not .EOF
Dim i As Integer
For i = 0 to !NMR.Count ' or SOMETHING - this is the problem
' this is irrelevant, I need to know how to iterate the list
Next i
.MoveNext
Loop
End With
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
I've also tried logic control such as
Do While(!NMR(i) <> vbNullString) since the individual components are strings, but no luck. This issues the same 3265: Item isn't found error. Same for a loop with this check Do While Not IsNull(!NMR(i))
Is there a way to tell how many objects are in the Field !NMR?
You need to assign the complex Field2 to a Recordset2 object and loop through it.
Sub Whatever()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsComplex As DAO.Recordset2
Dim qry As String
qry = "SELECT * FROM Experiments"
Set db = CurrentDb
Set rs = db.OpenRecordset(qry, dbOpenSnapshot)
Do While Not rs.EOF
Set rsComplex = rs("NMR").Value
rsComplex.MoveLast
rsComplex.MoveFirst
Dim i As Integer
For i = 0 To rsComplex.RecordCount - 1 ' Asker modified
Debug.Print rsComplex(0)
rsComplex.MoveNext
Next i
rsComplex.Close
Set rsComplex = Nothing
rs.MoveNext
Loop
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
In Access VBA I'm attempting to update a combo box's value based on a query.
Code:
Function updateComboBox()
Dim db As Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Dim user as string
user = environ("username")
strSQL = "SELECT [Name] FROM myQuery WHERE [UserName] = '" & user & "'"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
Me.MyComboBox = rs.Fields(0)
End If
End Function
The error I get:
You can't assign a value to this object.
How can I change the current value of the combo box?
It may be important to note that the combo box is a list of values populated from a query. It happens to be the same query strSQL is using. It is important that users can still choose other names, but it defaults to their own.
EDIT:
I ended up doing the following work around to make this work:
I set the default property of MyComboBox to be =[myTextBox].
Form_OnOpen --> Update myTextBox's value based on query. Refresh form so combobox displays myTextBox's value.
use this
for single data Me.MyComboBox.AddItem(***)
for collection
Me.MyComboBox.List = rs.Fields(0)
I have a an Access form with a parameter query (pink color). Here is my form:
When the user selects a species, Access uses the number generated by the option group (called "speciesSelection") to lookup the species name in a table, and it works. However, I would like to pass the species name to a parameter query, so that a record set can be the data source for the highlighted combo box (Combo12). However, when I select a species, the combo box is blank. Here is my code:
Private Sub speciesSelection_AfterUpdate()
Dim dbs As Database
Dim qdf As QueryDef
Dim rst As Recordset
Set dbs = CurrentDb
'Get species name of the current Cases instance'
Dim speciesChosen As String
speciesChosen = DLookup("Species", "tblSpeciesList", "ID=" & speciesSelection)
'Get the parameter query
Set qdf = dbs.QueryDefs("qryClinicalObservations")
'Supply the parameter value
qdf.Parameters("enterSpecies") = speciesChosen
'Open a Recordset based on the parameter query
Forms!inputForm.Controls!Combo12.RowSource = qdf.OpenRecordset()
End Sub
I created my query using the wizard. Here is a snapshot:
In the criteria section, I can manually enter a species when prompted (e.g. "cat"), and it works. But not with my VBA code...
Is there an obvious mistake? It seems that Combo12 is not recognized.
EDIT:
Here is my new code. Actually, Combo12 is in a subform called observationsSubform. Here is my code and new form. As you can see, the drop-down menu, but the options aren't visible:
Private Sub speciesSelection_AfterUpdate()
Dim dbs As Database
Dim qdf As QueryDef
Dim rst As Recordset
Set dbs = CurrentDb
'Get species name of the current Cases instance'
Dim speciesChosen As String
speciesChosen = DLookup("Species", "tblSpeciesList", "ID=" & speciesSelection)
MsgBox (speciesChosen)
'Get the parameter query
Set qdf = dbs.QueryDefs("qryClinicalObservations")
'Supply the parameter value
qdf!enterSpecies = speciesChosen
Set Me!observationsSubform!Combo12.Recordset = qdf.OpenRecordset()
A combo box's RowSource is a string property, so you can't assign a Recordset object to it. Assign your Recordset to the combo's Recordset property instead.
Since that is an object assignment, use the Set keyword.
Set Forms!inputForm!Combo12.Recordset = qdf.OpenRecordset()
If Combo12 and speciesSelection are both contained in the same form (inputForm), you can use this instead ...
Set Me!Combo12.Recordset = qdf.OpenRecordset()
I have a form with a subform. This subform displays the results of a query that is created dynamically (user enters criteria, I build the SQL, then update the querydef and display). Problem is since the columns are dynamic the width of the columns isn't working out, some are cutting off text.
Is there a way to programmatically loop through the columns (or do the same without loop) and set them all to bestfit width after the query is refreshed?
EDIT: Here is what my code looks like now:
CurrentDb.QueryDefs("SearchResults").sql = sql
CurrentDb.QueryDefs.Refresh
Dim qdf1 As DAO.QueryDef
Dim fld1 As DAO.Field
Set qdf1 = CurrentDb.QueryDefs("SearchResults")
For i = 0 To qdf1.Fields.Count - 1
Set fld1 = qdf1.Fields(i)
fld1.CreateProperty "ColumnWidth", dbInteger
fld1.Properties("ColumnWidth") = -2 'Throws error
Set fld1 = Nothing
Next i
Me.Child20.SourceObject = "Query.SearchResults"
You can set column widths like so:
Sub SetColumnWidth()
Dim qdf1 As DAO.QueryDef
Dim fld1 As DAO.Field
Set qdf1 = CurrentDb.QueryDefs("query3")
For i = 0 To qdf1.Fields.Count - 1
Set fld1 = qdf1.Fields(i)
fld1.CreateProperty "ColumnWidth", dbInteger
'very narrow indeed
'fld1.Properties("ColumnWidth") = 200
'Or -2 : Sizes the column to fit the visible text
'but it is not quite as useful as it would seem
fld1.Properties("ColumnWidth") = -2
Set fld1 = Nothing
Next i
End Sub
See also http://support.microsoft.com/kb/210427
So I've run into this same problem just now. I was fortunate enough to have half of my queries work and the other half not. I've been using this code:
Sub QueryData(strSQL As String)
Dim qryData As DAO.QueryDef
Dim intcount As Integer
Set qryData = CurrentDb.QueryDefs("DataQuery")
qryData.SQL = strSQL
qryData.CreateProperty "ColumnWidth", dbInteger
qryData.Fields(0).Properties("ColumnWidth") = 5760
DoCmd.OpenQuery "DataQuery", , acReadOnly
End Sub
Which generated the error on half of the queries I tried to run with it. I traced it back to this odd, but simple truth: Columns built using an Alias (i.e. all formula columns and expressions) kick out this error. If the column is just a straight data pull, it works fine. If the column is, however, a formulated display.... it spits the no columwidth property error.
Hopefully this'll help someone out! I know this questions about a year old, but it was the first result Google found for me on the topic.
I was able to make this grab the open forms and autofit the selected subform within that form. If you have multiple forms/subforms you would just call the function with the new names using the lines of code at the end of the function and pasting them in your program.
Public Function AutoSizeSbCtrl(frmNameTar, sbCtrlNameTar)
For Each frm In Forms
frmName = frm.Name
If frmName = frmNameTar Then
For Each frmCtrl In frm.Controls
frmCtrlName = frmCtrl.Name
If frmCtrlName = sbCtrlNameTar Then
For Each sbfrmCtrl In frmCtrl.Controls
sbfrmCtrlName = sbfrmCtrl.Name
On Error Resume Next
sbfrmCtrl.ColumnWidth = -2
On Error GoTo 0
Next sbfrmCtrl
End If
Next frmCtrl
End If
Next frm
' paste the lines below in your code where you want it to trigger (i did on an update)
'frmNameTar= "frm12345" ' where frm12345 is the name of the form the subform is in
'sbCtrlNameTar="sbfrm67890" ' where sbfrm67890 is the name of the subform you are trying to autofit
'auSize = AutoSizeSbCtrl(frmNameTar, sbCtrlNameTar)
'end paste
End Function
I have been looking for some time now, but I can't see the problem here:
Option Compare Database
Option Explicit
Private Sub cmd_Refresh_Click()
Dim sSQL_Select As String
Dim Qdb As Database
Dim Qry As QueryDef
sSQL_Select = "SELECT * FROM T_TIME_SCHEDULE"
Set Qdb = CurrentDb
Set Qry = Qdb.CreateQueryDef("QTS", sSQL_Select)
DoCmd.OpenQuery "QTS", acViewNormal
Me.F_Child_Result.Form.RecordSource = "QTS"
Me.F_Child_Result.Requery
Qdb.QueryDefs.Delete ("QTS")
Set Qdb = Nothing
End Sub
This line:
Me.F_Child_Result.Form.RecordSource = "QTS"
results in the following error:
Run-Time error '2467': The expression you entered refers to an object that is closed or doesn't exist.
The syntax should be correctly constructed, and I double checked the name of the subform.
Desipte, the error remains.
Do not set the record source to a query, just the SQL string.
sSQL_Select = "SELECT * FROM T_TIME_SCHEDULE"
''Set Qdb = CurrentDb
''Set Qry = Qdb.CreateQueryDef("QTS", sSQL_Select)
''DoCmd.OpenQuery "QTS", acViewNormal
Me.F_Child_Result.Form.RecordSource = sSQL
''Me.F_Child_Result.Requery
There is no need to requery, the form will be requeried when the record source is changed.
Note that even if you could set the record source to a query (which you can't), it would not be quoted.
FROM Chat
It appears that there are two mixed strands in the question. What is required is a means to set the Source Object of the subform control to a query, not the Record Source of a form contained by the subform control.
The source object can be set like so:
Me.F_Child_Result.SourceObject="Query." & sQueryName
If the Source Object is already set to the name of a saved query that is used only for this form, there is no need to reset it, you can just modify the sql of the query:
Set qdf = CurrentDB.QueryDefs("JunkQuery")
qdf.SQL = sSQL