I am very new to MS Access and yet have been working (loosely) on a DB for a while. We have a DB that tracks membership. There is a table with all of the member info in there. When new and current customers are added, or pay for the current year, the info is applied to a 'PaidYear' column. For years now, I have been adding a query listing the current years' members and adding a report that displays the output of the query.
I would like to create a report where I could (using a drop-down maybe) select the active year and other options such as City, Company Name, Phone Number, etc. Is there any way to simply set this up? It has to be easy enough for my replacements to intuitively use. ie:
Member Report for [Choose Year] <-- Dropdown
[City] [Company] [Phone] [Select Option]<--- Extra Options for reporting
I have been playing with it for a while and while I can get the design set up, I can't set up the functionality. Thank you so much!!!
Yup, if you want to filter down your report, you can write a little bit of VBA to open your report with a filter (you don't need to use a parameter query for this. It may be more efficient to do this at the query execution level, but as far as i've noticed, the performance is the same to just run the full query and filter it at runtime of the report open (Access might actually just do this behind the scenes, again, I don't really know.
Anyways, lets get to it. Here's a code snippet that you can kind of use as a starting point and adapt.
Create a button that says 'Run Report', let's call it cmdRunReport
In the On Click event for that button, you will put some code. I'm just writing this up now, so I might have some syntax errors (don't have Access on this PC).
dim multiple as boolean
dim filtering as string
filtering = ""
if me.yearDropdown is not null then
filtering = filtering + "[myYearField] = " & me.yearDropdown
multiple = true
end if
if me.cityDroPDown is not null then
if multiple then
filtering = filtering + "AND [myCityField] = '" & me.cityDropdown & "'"
else
filtering = filtering + " [myCityField] = '" & me.cityDropdown & "'"
set multiple = true
end if
end if
if me.CompanyDropDown is not null then
if multiple then
filtering = filtering + "AND [myCompanyField] = '" & me.CompanyDropdown & "'"
else
filtering = filtering + " [myCompanyField] = '" & me.CompanyDropdown & "'"
set multiple = true
end if
end if
DoCmd.OpenReport "yourReport", acViewPreview, , filtering
This is the basis of what you can do. I may have a couple if syntax errors and concatenated the filtering string incorrectly (untested), but that's where you could start.
In english, it just looks at your form's dropdowns that you use to filter. It checks if they are not null, and then concatenates their values into the "filtering" string. This string is used as a parameter in your OpenReport method.
Hope this helps.
Related
I am currently trying to help a friend out with their invoicing Access database. I have rarely ever used Access and I am having problems figuring out the location of where the form (frmEntry) is pulling its data from. I did not create this setup so I am unsure of how it works. I am trying to figure out where the address information is being pulled from for when a customer is selected in a drop down on a form. I checked the query and it is only pulling the CustomerID and CustomerName, no address. The table does have address fields but none of the customers in the table have any listed, yet there address is populated along with their name in the form.
I do see where there is another form (frmCustomer) that has customer and there addresses but I am not sure if the other form is pulling from here, and if so, why can I not find the addresses in any of the tables or datasheet views?
Any direction would be very much appreciated. My end goal is to obtain the customer information (address etc) so that I can insert it into a new database that I am working on
Your data contains linebreaks and a combobox only shows one line per record.
To show the data you can replace the linebreaks in rowsource.
SELECT Replace([CustomerName],vbCrLf, " ") as CName FROM table
' vbCrLf is the VBA constant for linebreaks (Cr - Carrige Return, Lf - LineFeed)
This is poor database normalization (imagine you want to search for a customer name that is equal to a city, e.g. Paris). Each line should be a separate field in table (and Postcode too). If there is a linefeed for every data (e.g. no street -> empty line), you can just split the data into the new fields.
'Put this code in a module
'Split function
Public function splitCustomerName(ByVal strCustomerName as String, ByVal index as long) as String
Dim arrCustomerName As Variant ' or declare a fixed array if you know the number of lines
arrCustomerName = Split(strCustomername,vbCrLf)
splitCustomerName = arrCustomerName(index)
End Function
The query
UPDATE table SET newCustomerName = splitCustomerName([table].[CustomerName],0)
, newCustomerStreet = splitCustomerName([table].[CustomerName],1)
, newCustomerCity = splitCustomerName([table].[CustomerName],2);
Just create the necessary columns for name, street and city, then run the query.
If you delete the CostumerName column and rename the table (e.g. newTable) you can create a query with the oldname of the table, that behaves like your old table.
SELECT *
, newCustomerName & vbCrLf & newCustomerStreet & vbCrLf & newCustomerCity as CustomerName
FROM newTable
My concept is this: There are three columns relating to a definite hierarchy. I would like our users to be able to input into the combo boxes in whichever order they'd like and have the information pulled in the other two combo boxes be reactive to this.
Example: Country / State / (County/Region/District/City) in a tracker; The sql referenced table with source info would be Country_Name, State_Name, County_Name
If one were to put in "Vienna" into County, one would have options for Georgia, Missiouri, etc. under state, and United States and Austria as options for Country (I have no idea the larger provincial structure for Austria to add them to a state field--this is meant as an analogous example).
If one were to put in "Virginia" under state one would get United States as an option for Country, and Various counties as options for County.
The hierarchy would be relatively normal for inputting a country, as that's the natural drill down.
I do understand how to do a cascading (one-way) combo-box. The problem lies with being unable to use a nested Iif in the control source, or being unable to temporarily amend the control source through _AfterUpdate cases--please excuse the pseudocode:
Private Sub State_AfterUpdate()
If Country = "" And County = "" Then
Me.Country.ControlSource = "SELECT Country_Name FROM Natl_Structure WHERE State_Name = " & Forms![Postal]![State]
Me.County.ControlSource = "Select County_Name FROM Natl_Structure WHERE State_Name = " & Forms![Postal]![State] & ";"
Elseif Country <> "" And County = "" Then
Me.County ControlSource = "Select County_Name FROM Natl_Structure WHERE (State_Name = " & Forms![Postal]![State]) AND (Country_Name = " & Forms![Postal]![Country] & ";"
...and flip it for the opposite case. And set up the opposite of the first case for if both were setup (not that it would be necessary at that point, but just to account for all cases). Then apply the same sort of measure to the other two combo boxes.
Any and all help would be appreciated.
You have to use IIF in your control sources to check if there is already a value in the other combo boxes or not.
Me.County.ControlSource = "Select County_Name FROM Natl_Structure WHERE 1=1" &
IIf(IsNull(Forms![Postal]![State]),"", " AND State_Name = " & Forms![Postal]![State]) &
IIf(IsNull(Forms![Postal]![Country]),""," AND Country = " & Forms![Postal]![Country])
Which one could be a better practice? In my situation, I need to check if a specific data exists in a table. I am iterating through an Excel file and verifying if a code there exists in my table using VB.NET. I have two options to do this (or if there is a better way to do this, I am open for suggestions).
First is to check it one by one, this code is executed per loop:
SQL = "SELECT TOP 1 * FROM Table1 WHERE Code = '" & codeFromExcel & "'"
rs = dbConn.Execute(SQL)
If Not rs.EOF Then
isFound = True
Else
isFound = False
End If
The other one is I load all the codes in a List(Of T)
Dim myList As New List(Of String)()
rs = Nothing
rs = dbConn.Execute("Select Code from Table1")
If Not rs.EOF Then
Do While Not rs.EOF
myList.Add(rs.Fields("Code").Value.ToString)
rs.MoveNext()
Loop
End If
Then check every record if it is in the List(Of T) while iterating in the Excel.
If myList.Contains(codeFromExcel) Then
isFound = True
Else
isFound = False
End If
I've been working with this kind of stuff most of the time and I want to know which one is the most efficient way to use. At the moment I only have a few records in my database. I want my code to be ready and efficient when the time comes that I need to deal with numerous records. Thanks in advance!
Additional info: The data doesn't need to be "fresh" as that table is meant for one-time entry only.
Personally I prefer to open as less connections to data base as possible.
So:
If the table is not very large (some hundred rows) I would go with the "cache" option.
Generally:
I would gather all excel codes in a list. ( excelCodes )
Then I would query something like Select Distinct Code from Table1 Where Code In ( excelCodesList ) and store it in a second list ( foundCodes ).
Then I would compare these lists.
I test it on a table with 6.143.993 rows.
To select just one column (description) to "cache" took 1'29".
On the other hand query like:
select distinct description from ItemDetail where description in ( 'abc','cddd','xxx' )
took 0'58".
UPDATE
An index on Code column might help with performance.
I am new to access but I have a problem. Take a look at the screenshot of my table relationships. I would like to create a form such that when someone enters the 'Student Code', the student code is populated in all three tables, rather than just one, saving time and confusion as one only needs to type it in once. Thank you everyone in advance. see http://i.stack.imgur.com/bHBqM.png
In the AfterInsert event of the Student Information form, run append VBA queries. With this, each time you add a new Student record, the corresponding student code will be inserted into the other related tables:
DoCmd.RunSQL "INSERT INTO [Parent Information] (Student Code)" _
& " VALUES (" & Forms!yourformname![Student Code] & ");"
DoCmd.RunSQL "INSERT INTO [Student Medical Conditions] (Student Code) " _
& " VALUES (" & Forms!yourformname![Student Code] & ");"
Alternatively, you can save the above queries as stored queries and run them via Macro (Open Query) or VBA (DoCmd.OpenQuery) -both still in AfterInsert form event:
DoCmd.OpenQuery "ParentInformationQ"
DoCmd.OpenQuery "StudentMedicalConditionsQ"
If your subforms are included on main form, add Requery commands to update subforms. With this, you will see blank rows appear in subforms, ready for data edit (NOT entry since you created the records in prior queries):
Forms!yourformname!yourparentinfosubformname.Form.Requery
Forms!yourformname!yourmedicalconditionssubformname.Form.Requery
I am fairly new to both Access and Databases and I'm struggling with how to add a new record into a table using a form, which gets its options from multiple other tables.
I have 4 tables which contain: a list of periods (tblPeriod), a list of pupils (tblNames), a list of subjects (tblAreaOfLearning) and a list of levels (tblLevels).
I want to create a form which will allow a user to select the period, then the pupil, then the subject, and then the level at which that pupil is attaining at that subject, and then add the ID's for all of these into a 5th table called tblMaster
I have no idea how to do this though? I have created a query linking everything to the master table by the ID's, but i have no idea how to create the form and the command to insert this into the new table?
All help appreciated
Here's an idea:
It looks like you can scrap your query idea, and do this instead...
Create a new form with 4 combo boxes:
cboPeriodID- user to select the period (recordsource tblPeriod query)
cboNameID- then the pupil (recordsource tblNames query)
cboAreaOfLearningID- then the subject (recordsource tblAreaOfLearning query)
cboLevelID- then the level at which that pupil is attaining at that subject (recordsource tblLevels query)
Then have a command but at the bottom of the
form with an Event Procedure to run the SQL to
insert the values into the master table.
Sub btnSubmit_Click()
dim strSQL as string
strSQL="INSERT INTO tblMaster (m_period_id,m_name_id,m_areaoflearn_id,m_level_id) VALUES (" & cboPeriodID & "," & cboNameID & "," & cboAreaOfLearningID & "," & cboLevelID & ")"
CurrentDB.Execute strSQL
End Sub