Wildcard in textbox - ms-access

I have two tables.
1.Test_Cap_Model1
2.Router
I have one report: ReportYield
In this report I made one control text box that contains code sql statement as below
This code will generate route value from router table if model value in report (its from query that generate from test_cap_model1 table) contain in router.[testmodel].
I tried to do in query but my problem is value of model in test_cap_model1 table are not the same in test model in router table. As example
Model value in test_cap table = 1471D3BTL-Non HW
but
Test Model Value in Router table = 1471D3BTL

Use DLookup for such tasks, and the Value property:
Private Sub Text10_Click()
Me!Text10.Value = DLookup("[Route]", "[Router]", "[Model] Like '" & Me!Model.Value & "*'")
' or:
' Me!Text10.Value = DLookup("[Route]", "[Router]", "[Model] Like '" & Me!Model.Value & "%'")
End Sub
There is nothing to requery. And do change your control names to something meaningful.

Related

Set default value or ignore Append Query Field that does not exist in MS-Access

I have a series of Excel Tools that work with an Access database (which is really the front end to a series of SharePoint lists). Each Excel Tool has an export function that creates a separate file (CSV) containing data to bring into the database. In the database, the CSV comes in as a local table, and an append query is run to add the contents of the local table to the SharePoint list. I am not fond of the architecture, but due to the robustness of my company's IT Security protocols, this is what I have to do.
There is a list of columns that COULD exist in the CSV, but whether they exist or not depends on the data entered into the workbook. For example, here are all the possible column headings:
kWh Savings
kw Savings
Natural Gas Savings
Water Savings
Fuel Oil Savings
Propane Savings
Depending on the customer/building/end use, some of the fields might not exist. For example, if this is a lighting calculator, no water savings will exist, and therefore that columns will not exist. Since the field does not exist, the append query throws a dialog box to ask me the parameter value. What I'm trying to accomplish is for it not to do this. I don't have a preference whether it just ignores the field, or if it defaults to 0. Any thoughts? Here are my queries that make the whole thing work:
[AppendFilter]
SELECT csv.*
FROM csv LEFT JOIN sharepoint ON (csv.FIM_Unique = sharepoint.FIM_Unique) AND (csv.[Building ID] = sharepoint.Building)
WHERE (((sharepoint.FIM_Unique) Is Null) AND ((sharepoint.Building) Is Null) AND ((csv.FIM_Unique) Is Not Null));
[AppendData]
INSERT INTO sharepoint ( FIM_Unique, FIM_Designation, [FIM Description], Safety_Factor, Building, [kWh Savings], [kW Savings], [Natural Gas Savings], [Water Savings], [Fuel Oil Savings], [Propane Savings] )
SELECT [AppendFilter].FIM_Unique, csv.[FIM Designation], csv.[FIM Description], csv.[Safety Factor], [AppendFilter].[Building ID], [AppendFilter].[kWh Savings], [AppendFilter].[kW Savings], [AppendFilter].[Natural Gas Savings], [AppendFilter].[Water Savings], [AppendFilter].[Fuel Oil Savings], [AppendFilter].[Propane Savings]
FROM [AppendFilter]. INNER JOIN csv ON [AppendFilter].FIM_Unique = csv.FIM_Unique
WHERE ((([AppendFilter].FIM_Unique) Is Not Null) AND (([AppendFilter].[Building ID]) Is Not Null));
I have tried DoCmd.SetWarnings False when running the query, but these warnings appear immune to that!
Could use VBA to dynamically build and execute SQL action. One way is with DAO TableDefs to iterate through table fields and build a string of field names and use string variable to build SQL statement. I don't think need to join CSV with AppendFilter. For one thing, doing so results in a SELECT query with duplicate field names and that will cause confusion unless fields are prefixed with table name.
However, gets complicated if field names are not same in source and destination tables. I see some variation with underscores and Building ID as opposed to Building. Since there are only 3 fields with variation, build string variable with conditional code to check for particular field names and make adjustment as appropriate.
Dim db As DAO.Database, td As DAO.TableDef, fd As DAO.Field, strD As String, strF As String
Set db = CurrentDb
Set td = db.TableDefs("csv")
For Each fd In td.Fields
strF = fd.Name
If strF = "FIM Designation" Or strF = "Safety Factor" Then strF = Replace(strF, " ", "_")
If strF = "Building ID" Then strF = "Building"
strD = strD & "[" & strF & "],"
Next
strD = Left(strD, Len(strD) - 1)
db.Execute "INSERT INTO sharepoint(" & strD & ") " & _
"SELECT * FROM csv " & _
"WHERE FIM_Unique IN(SELECT FIM_Unique FROM AppendFilter)"
I strongly advise not to use spaces nor punctuation/special characters in naming convention. Underscore is only exception but I never use that either.

Junction between categories

I am making a database for a freelance sign language interpreter. I have a subject table tblClient which holds information regarding the freelancer's clients. There is a lookup table tlkClientClientType showing the various types of clients (categorized on condition -- deaf, deaf/blind, etc). There is also a table called tlkClientModality. In this table are the different types of sign language offered by the interpreter. You can pick the client's preferred modality from the Client table.
Now the tricky part. There are certain modalities (basically just flavors of sign language) that should not be available to pick if the client is a certain type. For example, you cannot use "regular" (visual) sign language with someone who is deaf/blind. This is because regular sign language depends on the person being able to see. Instead, one would use "Tactile ASL" which is a hand-over-hand version of sign language. Basically, I want to limit the modality list based on the client type picked.
To start off my associations, I am making a junction table between the tlkClientClientType and tlkClientModality tables. Here, I will create the correct allowable pairs of client types and modalities. In addition, there is not really a necessary "junction field" to justify this relationship. I am considering making a "dummy field" as a way to still justify such a relationship.
Later, in a form for the client, I will edit the row source query on the modality combo box to be dependent on the choice selected in the client type box. This would be accomplished by checking what records in the junction table match the choice in the client type combo box.
Am I on the right track here? Making a junction table between two lookup tables seems weird. Is there anything wrong with it?
Note -- I would like to stay away from VBA, but I am up to writing macros to accomplish these goals. Any thoughts would be appreciated.
A photo of the relationships is given below.
Relationshipsphoto
Keep your tables in whatever normal form works for your business case. Your choice just changes how the information is stored which means you have to change how you write your queries as the information may be located in a different place. I chose a many to many relationship between client and clienttype.
You want to set the modality combobox contents from the client combobox. This is a problem of the view not the model hence we change the view and not the organization of the data. the key is to set the recordsource of the modality combobox from the afterupdate event of the client combobox.
Showing the results first: if the client is deaf we get:
if the client is blind or blind and deaf we get:
Private Sub cmbClient_AfterUpdate()
Dim RegularASL As Integer: RegularASL = 1
Dim TactileASL As Integer: TactileASL = 2
Dim ClientID As Integer: ClientID = Me.cmbClient
If ClientisBlindandDeaf(ClientID) Then
cmbModality.RowSource = "SELECT * FROM tlkClientModality WHERE ClientModalityID = " & TactileASL
ElseIf isClientBlind(ClientID) Then
cmbModality.RowSource = "SELECT * FROM tlkClientModality WHERE ClientModalityID = " & TactileASL
Else
cmbModality.RowSource = "SELECT * FROM tlkClientModality WHERE ClientModalityID = " & RegularASL
End If
cmbModality.Requery 'reload cmbmodality data
Me.Refresh 'repaint cmbmodality
End Sub
Public Function isClientBlind(ClientID As Integer) As Boolean
'Get rid of * in names vba can't parse the *
Dim isblind: isblind = 2
Dim ClientClientTypeID As Variant 'allows null return type
ClientClientTypeID = DLookup("ClientClientTypeID", "tlkClientClientType", "ClientID = " & ClientID & " AND ClientTypeID = " & isblind)
If IsNull(ClientClientTypeID) Then
isClientBlind = False
Else
isClientBlind = True
End If
End Function
Public Function isClientDeaf(ClientID As Integer) As Boolean
Dim isdeaf: isdeaf = 1
Dim ClientClientTypeID As Variant 'allows null return type
ClientClientTypeID = DLookup("ClientClientTypeID", "tlkClientClientType", "ClientID = " & ClientID & " AND ClientTypeID = " & isdeaf)
If IsNull(ClientClientTypeID) Then
isClientDeaf = False
Else
isClientDeaf = True
End If
End Function
Public Function ClientisBlindandDeaf(ClientID As Integer) As Boolean
If isClientBlind(ClientID) And isClientDeaf(ClientID) Then
ClientisBlindandDeaf = True
Else
ClientisBlindandDeaf = False
End If
End Function
note: cmbModality is set up just like it is bound to the entire tlkModality table then the record source changes are used to filter it in effect.

Update a multi-valued column through checkbox values

I have recently started using Access with VBA.
Problem Statement : There are 4 optional subjects say Maths,Economics,Computer Science,Home Science. A column called 'Subject' for each student stores the optional subjects(the number of subjects varies with each student).
What I did : I made a multi-valued field in the table called 'Subject' using the lookup wizard in design view. In the form view, I have these 4 checkboxes listed to select and submit . When I try writing an update statement for field 'Subject' using the checkbox values it gives me an error.
Is there some other way to solve this problem or can I correct the method I have used?
Thanks !
Create a new table with 2 fields; Student ID and Subject. Write all subjects into that table. When you need to return them, either use a subform (linked on StudentID) or create a pivot table if necessary.
Multi-Value columns are not optimal, they create a lot of problems. If you absolutely must use one, then you need to parse the data when you extract it. When you first write it, you need to write a statement like;
MyStr = ""
If Me.Checkbox1 = True then
MyStr = MyStr & "SomeValue, "
EndIf
If Me.Checkbox2 = True then
MyStr = MyStr & "SomeValue2, "
EndIf
If Me.Checkbox3 = True then
MyStr = MyStr & "SomeValue3, "
EndIf
If Me.Checkbox4 = True then
MyStr = "SomeValue4, "
EndIf
If Len(MyStr) > 2 Then
MyStr = Left(MyStr, Len(MyStr) - 2)
EndIF
What this does is continually append the values you assign to the checkboxes, and then removes the last 2 characters (which should be ", ") so you have a complete string. Then you write MyStr to your Subject field.
When you go to read this info, you'll need to parse it out into an array and work backwards, determining which checkbox should be marked based on the values in your array.

how to change the date format as it not the same with the database

As the coding show at textbox 18/02/2012 (for example). but in database it is 02/18/2012.
TxtReqDate.Text = Calendar.SelectionStart.ToString("dd/MM/yyyy")
cboTermCode.Focus()
Calendar.Visible = False
But when i want to retrieve the related data for the date using coding below :
sqlCbo2 = "SELECT DISTINCT gear_code FROM gear WHERE est_start_date='" & TxtReqDate.Text & "'"
it say invalid date.
i cannot change the date format in the database.
if i change the code TxtReqDate.Text = Calendar.SelectionStart.ToString("dd/MM/yyyy") TO TxtReqDate.Text = Calendar.SelectionStart.ToShortDateString as it will appear 02/18/2012 at the textbox the data will appear but i want 18/02/2012 to appear at the textbox.
You could make use of STR_TO_DATE function to parse that data and transform it into a Date value.
Use it like this (correct any syntactical error):
where do i should put it. example??
sqlCbo2 = "SELECT DISTINCT gear_code FROM gear WHERE est_start_date=STR_TO_DATE('" & TxtReqDate.Text & "','%d/%m/%Y')"
This example is actually using the OleDbCommand but the syntax is similar for the SqlCommand: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx
Dim comm As New OleDb.OleDbCommand(reportQueryString, conn)
comm.Parameters.Add("[ReportDate]", OleDbType.Date)
comm.Parameters("[ReportDate]").Value = dateVariable
You can see that when you add the parameter, you specify the dataType the command should convert your data type to. This way you're not doing raw string manipulation yourself.

Display Parameter(Multi-value) in Report

Can anyone tell me how to display all the selected value of my multi value parameter in SSRS report. When giving parameter.value option it gives error.
You can use the "Join" function to create a single string out of the array of labels, like this:
=Join(Parameters!Product.Label, ",")
=Join(Parameters!Product.Label, vbcrfl) for new line
I didn't know about the join function - Nice! I had written a function that I placed in the code section (report properties->code tab:
Public Function ShowParmValues(ByVal parm as Parameter) as string
Dim s as String
For i as integer = 0 to parm.Count-1
s &= CStr(parm.value(i)) & IIF( i < parm.Count-1, ", ","")
Next
Return s
End Function
Hopefully someone else finds this useful:
Using the Join is the best way to use a multi-value parameter. But what if you want to have an efficient 'Select All'? If there are 100s+ then the query will be very inefficient.
To solve this instead of using a SQL Query as is, change it to using an expression (click the Fx button top right) then build your query something like this (speech marks are necessary):
= "Select * from tProducts Where 1 = 1 "
IIF(Parameters!ProductID.Value(0)=-1,Nothing," And ProductID In (" & Join(Parameters!ProductID.Value,"','") & ")")
In your Parameter do the following:
SELECT -1 As ProductID, 'All' as ProductName Union All
Select
tProducts.ProductID,tProducts.ProductName
FROM
tProducts
By building the query as an expression means you can make the SQL Statement more efficient but also handle the difficulty SQL Server has with handling values in an 'In' statement.