I was wondering if you can use a Combo Box on a form in access as a data entry for a query.
For instance, I have a form based on a query and I want to use a Combo Box to edit a field that is blank on the query end based on the values that are in the Combo box, similar to a drop-down box in a query or table.
I created a Column for the data values to be entered and the values will go into a given based on the record page the combo box selection is made on.
I can add additional information if needed.
I'm a bit confused here, but it sounds like you want to use a pass a parameter into a query. Is that right? Something like this, right.
If this is going in the direction you want, read through the links below, and download the sample files at the bottom of the URL.
http://www.fontstuff.com/access/acctut17a.htm
http://www.fontstuff.com/access/acctut18.htm
You need to create a base query that will contain all of your data, Then for each combo box you will need to recreate and run the query. I usually program this on the after update event. I also create and store the query so that when a user returns to the form it returns to the same data as before. Just be warned that this could cause errors when the query is executing and updating subforms.
Example on your Form
'Assumes Combo1 has the data to filter and Table1 is source and Query1 exists
Private Sub Combo1_AfterUpdate()
Dim Obj_QueryDef As Object
Dim Temp_QueryName As Variant
'Save Query Name for refreshing Form Data (Query will be overwritten!)
Temp_QueryName = Me.Form.RecordSource
'dereference the query definition object that already exists
Set Obj_QueryDef = Me.Form.Application.DBEngine.Workspaces(0).Databases(0).QueryDefs(Temp_QueryName)
'For Number Key
Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE [Field1] = " & Combo1 & ";"
'Reset the Record Query then repaint the form
Me.Form.RecordSource = Temp_QueryName
Me.Form.Repaint
End Sub
'To add Add More Combo Boxes, After update Regenerate SQL adding & " AND Field2 = " Combo2 & ";"
'Or better yet Create a Function that handles the SQL statement
'You can use the same idea to limit the items that appear in the dropdown selection if you update
' the record source for the combobox
'reference for different data types:
'String Data 'String Value' use chr(39) = '
'Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE Field1 = " & Chr(39) & Combo1 & Chr(39) & ";"
'For Date Use #Date Time# Use chr(35) = #
'Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE Field1 = " & Chr(35) & Combo1 & Chr(35) & ";"
Related
The management staff in my department has been asked to record any and all errors committed by our team. I created a database to store them and a simple form for the management team to use to record each error. Several fields are referencing other tables such as Staff, issue category, root cause, etc...
We need to be able to search the forms for specific records to either update or review, and I have found the best way for us to search is by filtering the forms based on the individual who committed the error. Here is the code that I am using for the search button:
Private Sub SearchRecord_Click()
Dim Search As String
Search = InputBox("Please enter who committed the error", "Name", ErrorMadeBy)
If Search = "" Then Exit Sub
Me.Filter = "ErrorMadeBy = """" & Search & """
Me.FilterOn = True
End Sub
The filter works great, but instead of filling out the name, you have to use the ID number in the Staff table when filling out the Input Box. I'd like to be able to input the name (or part of the name) instead of having to have everyone memorize the ID numbers from the staff table.
What I do in these cases?
Fire up the query builder - left join in those extra tables (with the text parts in place of the id).
So be it a part number, quote number for a project etc? Just left join in those tables in the query builder and save that query.
Now, your search box and code can be somthing like:
dim rst as DAO.RecordSet
strSQL = "Select * from MyCoolQuery where PartName like '" & txtPartName & "*'"
set rst = currentdb.OpenRecordSet(strSQL)
if rst.EOF = false then
me.RecordSet.FindFirst "PartID = " & rst!PartID
end if
In fact, you can even often left join in those extra tables right into the current form, and even use ctrl-f to search if the said text boxes are place on the given form. However, I tend to separate out the search process from the actual form. So I might build a search form, say like this:
So in above, they can search say by a Hotel name, or project name or whatever. Becuase the one query has the text parts, then they can be searched. But I put the query results into a grid and each row of course has the PK ID, and thus a search could result in 10 matches, they are now free to click on any row - and i jump to the form to display the one records.
eg: the row click button does this:
docmd.OpenForm "frmTourBooking",,,"ID = " & me!ID
So, this makes for a great work flow. Users search, see, pick and then jump to the form with the one record, and then close and return back to search form - and often it has the several "hits" from that search that you want to work on anyway.
For my College assignment, I have to create a database in Access, I have done 99% of my database, apart from this section which I'm stuck on.
In my DB, I have a tickets table, which contains records on order information and a field containing a date. For my assignment, I have to create a Form which reads from a Query.
For example, in my Form i have already created i have 2 Combo boxes with the dates already pulled from the Query. I need to be able to drop down one of the boxes and input 1 date, and then drop down the other box and select a different date, press a button and it generate me a Report.
The part I am asking for help on is the expression which is used to look up the data inside the Query. I tried using this expression, which Access said was too complicated.
[Forms]![frmOrdersBetweenTwoDates]![Combo33] And [Forms]![frmOrdersBetweenTwoDates]![Combo36]
My full SQL query is:
SELECT tblTickets.CustomerID, tblCustomers.FullName, tblCustomers.AddressLine1, tblTickets.OrderNumber, tblTickets.OrderDate
FROM tblCustomers INNER JOIN tblTickets ON tblCustomers.[CustomerID] = tblTickets.[CustomerID]
WHERE ((("WHERE [OrderDate]") Between [Forms]![frmOrdersBetweenTwoDates]![Combo52] And [Forms]![frmOrdersBetweenTwoDates]![Combo54]));
My expression/query now returns the report, but there is no data inside the report. How could i fix this?
Cheers.
Should post the complete query statement. Expect the filter clause should be like:
WHERE [date fieldname] BETWEEN [Forms]![frmOrdersBetweenTwoDates]![Combo33] AND [Forms]![frmOrdersBetweenTwoDates]![Combo36]
However, I don't use dynamic parameterized queries. I prefer to use the WHERE argument of OpenReport (same for OpenForm), in VBA:
DoCmd.OpenReport "report name", , , "[date fieldname] BETWEEN #" & Me.Combo33 & "# AND #" & Me.Combo36 & "#"
Setup
Access 2007
This is a simplified scenario. It is nearly identical to my actual use case just easier to explain. I have a many to many relationship between movies and genres the table structure is below.
Table: movies
id autonumber
name text
Table: genres
id autonumber
name text
Table: movie_genres
movie_id number
genre_id number
I would like a form that allows me to list all genre's for a given movie. But also allows me to create new genre's without opening a separate form. Similar to the way free tagging works in a cms website like Drupal or Wordpress.
My Attempt 1
I have successfully created a form that allows me to display all tags using a sub-form pointing to the movie_genres table and a combo box pointing to the genre table. This form setup also allows me to select existing values as new genres. It does not however allow me to create new genre's.
In this form if I type a value not present I get the warning "The text you entered isn't an item in the list."
If I attempt to change the combo box to "Limit To List: No" I get the warning: "The first visible column... isn't equal to the bound column." If I make the first visible column the bound column the combo box simply displays numbers and not names, which is silly because the information is there either way.
The form for this simplified case looks like:
My attempt 2
I can also create a subform that points to both the movie_genres and genres tables with a regular textbox pointing to genre name. This allows me to create new values but it does not let me select from existing values. No pic of this one.
Creating a combo box on this form act identical to the second form.
The question again
How can I create a movie form item that supports both creation and listing existing genres?
You can easily add new values to the list of genres using 'NotInList' event. Leave Limit To List: Yes and use code similar to code below:
Private Sub GenreName_NotInList(NewData As String, Response As Integer)
' Prompt user to verify they wish to add new value.
If MsgBox("Genre '" & NewData & "' is not in list. Add it?", _
vbOKCancel) = vbOK Then
' Set Response argument to indicate that data
' is being added.
Response = acDataErrAdded
' Add string in NewData argument to row source.
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO Genres (GenreName) SELECT '" & NewData & "';"
DoCmd.SetWarnings True
Else
' If user chooses Cancel, suppress error message
' and undo changes.
Response = acDataErrContinue
Me.GenreName.Undo
End If
End Sub
I am currently at a loss for what to do in my current project. I am creating a form that will pull up a customer’s data. It is possible the customer could have more than record, and I have three different distinct fields to help narrow to that exact customer. We have an ID field which would be primary key, their SSN which only relates to them, and their account # which also only relates to them. I am curious if there is a way for the form to not be populated when I start it, and either be able to type in a text box one of these 3 and have them linked to a query that will search for that record and fill the form with the data, or have it pull up a parameter box(can do, but cant get the data to populate in form view) I am not having any luck with using the command buttons, either they don’t work the way I want them to, or the data gets pulled up in a datasheet, instead of my form.
I can manually filter on the ID numbers to find the record, but I’d rather make this user friendly for future users.
Also the form currently houses 95 fields of data, but they fit very comfortably in the form.
Access 2010
You can certainly have the form not populated. Create your three textboxes and a "go" button.
The code for the go button would be something like:
If Not IsNull(Me.txtID) Then
sWhere = sWhere & " Or ID=" & Me.txtID
End If
If Not IsNull(Me.txtSSN) Then
''If SSN is text, you need quotes
sWhere = sWhere & " Or SSN='" & Me.txtSSN & "'"
End If
If Not IsNull(Me.txtAccountNo) Then
sWhere = sWhere & " Or AccountNo=" & Me.txtAccountNo
End If
sWhere = "WHERE 1=1 " & sWhere
sSQL = "SELECT Each, Field, Name FROM TableName " & sWhere
Me.RecordSource = sSQL
The above is typed, not tested, and is only one approach. You could also use comboboxes built with the wizard to select a customer on your form, you could have a previous form where you fill in a customer and open your form from that, you could just filter your form based on what is filled in and so on.
As far as I know, there are rules about storing SSNs, so you will need to be careful.
I'm still working with the same database in which the table name stays the same but the field names will be different for each use. I'd like to create an option on the main form where the user clicks a command button to display a list of fields in the table (named'Scrubbed'), then selects a field from the list, and immediately a window opens up displaying the distinct field values. Can this be done?
You can do a lot with VBA, including creating a query on the fly. Comboboxes and listboxes have a RowSource option Field List, which will list the fields in your table.
Edit
Here is your code with a few changes:
Private Sub Command206_Click()
Dim strSQL As String
Dim strScrubbedValue As String
''I suspect this is running in the form, so Me
strScrubbedValue = Me.ComboListScrubbedFields
''Where the table is called Scrubbed
strSQL = "SELECT DISTINCT " & strScrubbedValue & " FROM Scrubbed"
''No need to execute, it is just a row source
''DoCmd.RunSQL strSQL
Me.Combo213.RowSource = strSQL
End Sub
I suggest you name your controls with meaningful names.