Junction between categories - ms-access

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.

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.

Wildcard in textbox

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.

Access will only retrieve data from 1 table in a join with no where clause

I am using MS-Access to get information from 2 tables. I have used inner join, left, right, and outer with all variations, and it will either pull 1 row when 316 are expected, all data for the fields in test with no values for the fields from test 1, or all data for fields from test 1, and no values for test. How do I resolve this? The actual fields had to be changed for privacy, but the below is the exact layout.
SELECT [TEST].a,
[TEST].b,
[TEST].c,
[TEST 1].[D],
[TEST].E,
[TEST].F,
[TEST].G,
[TEST].H,
[TEST 1].[I],
[TEST].J,
[TEST].K,
[TEST 1].L,
[TEST 1].[M]
FROM [TEST 1]
INNER JOIN [TEST] ON [TEST 1].[ID] = [TEST].[CLAIMSNO];
This is a data-validation and debugging exercise, so if you can't share concrete example data then there is really no definite answer to this question. Technically it may not be answerable according to common StackOverflow standards, but I feel generous right now.
Since the joined fields are text, there are various possibilities keeping them from matching: extra spaces, null-terminated strings, case sensitivity (although Access by default should be case insensitive), wide (Unicode) vs narrow (ASCII, UTF-8) encoding, etc. You did not reveal where the data came from, nor how it was loaded into the database, so I make no assumptions. In order to understand the data and determine the reason for the failed matches, you need to investigate the details of the strings. You could likely narrow the problem by investigating the source of the data values and understanding the range of possible characters, encoding, string termination, etc.
Since you are already having troubles matching data AND since you already indicated that the original tables had no primary key or indexes, I highly recommend adding a new AutoNumber field with a unique index to each table, perhaps named [AID] (for AutoNumber ID). Do this even if you have added indices to existing columns. This will at least provide a reliable "handle" to select and refer to a particular row while debugging the other columns.
The big idea is to use VBA or other built-in functions to inspect and report on various attributes of the string values. There are just too many ways you could do this, but my preference is to create a public VBA function in a normal VBA module and then call this function from an SQL query. Although you could do this for every row, instead I suggest manually choosing rows from each table which you think should match... record the [AID] value for each row. If the manually-selected rows don't result in anything enlightening, then run it against an entire table and see what interesting results you get.
Consider these functions:
Public Function CheckSpaces(val As Variant) As String
Dim result As String
If IsNull(val) Then
result = "Null"
ElseIf VarType(val) = VbVarType.vbString Then
If Len(val) = 0 Then
result = "Empty String"
Else
Dim temp As String
Dim n As Integer, m As Integer
n = Len(val)
result = "Length " & n
temp = LTrim(val)
m = Len(temp)
If n <> m Then
result = result & " AND " & (n - m) & " left spaces"
End If
temp = RTrim(val)
m = Len(temp)
If n <> m Then
result = result & " AND " & (n - m) & " right spaces"
End If
End If
Else
result = "Not a string!"
End If
CheckSpaces = result
End Function
Public Function NullChar(val As Variant) As Boolean
Dim result As Boolean
result = False
If Not IsNull(val) Then
If VarType(val) = VbVarType.vbString Then
If InStr(val, vbNullChar) > 0 Then 'vbNullChar = Chr(0)
result = True
End If
End If
End If
NullChar = result
End Function
And execute queries similar to the following. Let's say that [Test 1] row AID = 10 has [ID] == 'name'. Likewise, imagine row AID == 20 of [Test] has [CLAIMSNO] = ' name ':
SELECT [ID], CheckSpaces([ID]), NullChar([ID])
FROM [TEST 1]
WHERE [AID] = 10
and
SELECT [CLAIMSNO], CheckSpaces([CLAIMSNO]), NullChar([CLAIMSNO])
FROM [TEST]
WHERE [AID] = 20
Compare the returned values. Is there anything that indicates a failed match?

Filtering Information in A Report Using VBA-Access - Finding the next avaliable null field

I'm trying to organize information in a report in a certain way
I have 9 fields that will be used to store the label for the information
and another 9 fields that store the actual value of the information.
Whenever a field is full, I want it to place the information in the next available field. So that there will be no blank fields in between information.
The issue is that when trying to run this code which triggers two functions, it is not updating the report I referenced to in the two functions, nor is it pulling information from SpecSheetQuery, and I can't figure out why. My guess is that I'm not referencing the information correctly. I also have to figure out how to run this code individually on each record.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Servings = DLookup("[servings]", "SpecSheetQuery")
Calories = DLookup("[calories]", "SpecSheetQuery")
If Not IsEmpty(Servings) Then
OpenSlot ("Servings")
OpenFact (Servings)
End If
If Not IsEmpty(Calories) Then
OpenSlot ("Calories")
OpenFact (Calories)
End If
End Sub
OpenSlot function, puts title/label of information into field1 ( which is a field on the report) on the first null/empty field. so that no space is left blank.
Option Compare Database
Option Explicit
Public Function OpenSlot(Slot As String)
Debug.Print Reports!SpecSheet!field1.Value
If Reports!SpecSheet!field1.Value = Null Then
Debug.Print "Slot1"
Slot = Reports!SpecSheet!field1.Value
Debug.Print Reports!SpecSheet!field1.Value
ElseIf Reports!SpecSheet!field2.Value = Null Then
Reports!SpecSheet!field2.Value = Slot
End If
End Function
OpenFact function, puts fact into nut1 ( which is a field on the report) on the first null/empty field. so that no space is left blank.
Public Function OpenFact(fact As String)
If Reports!SpecSheet!nut1.Value = Null Then
Reports!SpecSheet!nut1.Value = fact
Debug.Print Reports!SpecSheet!nut1.Value
ElseIf Reports!SpecSheet!nut2.Value = Null Then
Reports!SpecSheet!nut2.Value = fact
End If
End Function
I'm trying to store this information into this report.
Any help would be greatly appreciated.

Using a User-Created Function in VBA to output criteria in access query

In short: I have a user-created function (gettargetTemp(targetTemp_input) with one input that, upon function evaluation, I would like to be able to call the function in an Access query (design view) criteria field and have it represent the criteria string that I want evaluated when the query is run.
i.e. targetTemp_input = 1450 - this value is assigned from a form (and can vary)
Access Query
Field: Pad Temp
Criteria: gettargetTemp("targetTemp_input")
Criteria possibilities:
- Records within 100 degrees of the targetTemp_input value which
typically in Access query design is: Between 1350 and 1550 Or Is Null
- All of the records
The code that makes sense to me is:
Public Function gettargetTemp(targetTemp_input)
If Forms![Parameter Confirmation].tempCheck = True Then
gettargetTemp = "Between " & (targetTemp_input - 100) & " AND " _
& (targetTemp_input + 100)
Else
End If
End Function
The outcome of this typically results in a data mismatch error from Access. Is there a way to accomplish what I'm trying to do? Or maybe do it better? I'm not a programmer, but I have a pretty good technical background.
At present, you're trying to have the VBA function form an expression that does the wanted test, rather than doing the test itself. You need to change that:
Function IsTempOK(Temp) As Boolean
Dim ParamForm As Access.Form, TargetTemp As Long
Set ParamForm = Forms![Parameter Confirmation]
If ParamForm.tempCheck Then
If IsNull(Temp) Then
IsTempOK = True ' or False, if that's what you want
Else
TargetTemp = ParamForm.TargetTemp ' or wherever this is defined
IsTempOK = (Temp >= TargetTemp - 100) And (Temp <= TargetTemp + 100)
End If
Else
IsTempOK = True
End If
End Function
In the query definition, the WHERE clause should now use IsTempOK, passing the Temp field as the parameter.