Converting check boxes to words - ms-access

I added check boxes to my MS Access form. Now I am trying to run a report based on those check boxes.
They are about 6 check boxes. How can I translate the check boxes from check marks to words?
An example of this would be on the form. Pizza toppings(onions,sausage,olives,cheese,chicken,mushrooms).
I need to be able to turn the check boxes from 6 individual check-boxes to make one toppings fields.
Also it is to late to change the table to use some form of list box.
Thank you.

Put a new text field into your report with a control source similar to the following:
=iif([onions],"Onions ", "") & iif([sausage],"Sausage ") & iif([olives],"Olives ","")
And so on...
If you would like to have commas in between the ingredients, you could also use something like the following:
=Concat(", ", iif([onions],"Onions", ""), iif([sausage],"Sausage"), iif([olives],"Olives ",""))
And put a Concat() function similar to this in one of your Modules:
Public Function Concat(Delimiter As String, ParamArray Strings()) As String
Dim s As Variant, ret As String
ret = ""
For Each s In Strings
If Not IsNull(s) And Not IsEmpty(s) And s <> "" Then
If ret = "" Then
ret = s
Else
ret = ret & Delimiter & s
End If
End If
Next
Concat = ret
End Function

You want something like this:
If chkTopping1 = True then MyTopping1 = "Bacon"
If chkTopping2 = True then MyTopping2 = "Sausage"
If chkTopping3 = True then MyTopping3 = "Pepperoni"
If chkTopping4 = True then MyTopping4 = "Mushrooms"
If chkTopping5 = True then MyTopping5 = "Meatballs"
If chkTopping6 = True then MyTopping6 = "Olives"
If Not IsNull(MyTopping1) then
AllToppings = MyTopping1 & ", "
If Not IsNull(MyTopping2) then
AllToppings = AllToppings & MyTopping2 & ", "
If Not IsNull(MyTopping3) then
AllToppings = AllToppings & MyTopping3 & ", "
If Not IsNull(MyTopping4) then
AllToppings = AllToppings & MyTopping4 & ", "
If Not IsNull(MyTopping5) then
AllToppings = AllToppings & MyTopping5 & ", "
If Not IsNull(MyTopping6) then
AllToppings = AllToppings & MyTopping6
AllToppings is now your text field. I'm positive there's a more graceful way to do this, but my brain is kinda burnt after a long day at the office.

Related

List Field field don't show a value

When I paste a value into a combo box, it is not displayed when I paste it on load. Although when inserting a value without passing an argument into the form, everything works. How to fix it?
The first picture shows the problem itself. The second shows that everything will work without passing an argument to the form! And in the drop-down list itself, everything is displayed
' Populating a list of values
If Not Me!PostField.RowSource = "" Then Me!PostField.RowSource = ""
Me!PostField.ColumnCount = 2
Me!PostField.ColumnWidths = "0; 25"
Set QueryRecordset = CurrentDb.OpenRecordset("SELECT tblPost.PostCode, tblPost.PostName FROM tblPost;")
Do Until QueryRecordset.EOF
Me!PostField.RowSource = Me!PostField.RowSource & QueryRecordset("PostCode").Value & ";" & QueryRecordset("PostName").Value & ";"
QueryRecordset.MoveNext
Loop
QueryRecordset.Close
' Inserting user data
QueryObj = CurrentDb.QueryDefs("qrsCurrentEmployeeInfo")
QueryObj![EmployeeCode] = CInt(Me.OpenArgs)
Set QueryRecordset = QueryObj.OpenRecordset
Do Until QueryRecordset.EOF
Me!Title.Caption = QueryRecordset("LastName").Value & " " & QueryRecordset("FirstName").Value & " " & QueryRecordset("MiddleName").Value & " (Ðåäàêòèðîâàíèå)"
Me!LastNameField.Value = QueryRecordset("LastName").Value
Me!FirstNameField.Value = QueryRecordset("FirstName").Value
Me!MiddleNameField.Value = QueryRecordset("MiddleName").Value
Me!SexField.Value = QueryRecordset("Pol").Value
Me!IndividualTaxNumberField.Value = QueryRecordset("InduvialNumber").Value
Me!SalaryField.Value = QueryRecordset("Salary").Value
Me!CompanyField.Value = QueryRecordset("CompanyCode").Value
Me!LegalAddressField.Value = QueryRecordset("LegalAddress").Value
Me!ActualAddressField.Value = QueryRecordset("ActualAddress").Value
Me!PhoneField.Value = QueryRecordset("Phone").Value
Me.MailField.Value = QueryRecordset("[E-Mail]").Value
Me!PostField.Value = QueryRecordset("EmployeePost").Value
QueryRecordset.MoveNext
Loop

Access form - update one field filter from code without conflicting existing filters

I have a subform, and it has a field with code source (custom VBA function doing a lookup). The filter button on that field doesn't work (Access by design). My users will want to use filters for this field and also for other fields.
As a workaround, I have added 3 checkboxes. If a user clicks any one of these checkboxes, filters get applied to the subform based on the function field.
The problem is, this removes all the other currently applied filters from the subform. This is not nice towards my users.
Is there a way to add and remove one field criteria to filtering without ruining the rest of the filter?
I have tried brute forcing it, but I gave up. When a filter gets added the normal way, many parentheses and AND words get added. My little filter text can be anywhere in a maze of filter criteria string. So using text functions to find it and manipulate it seems to be big, slow, stupid, unstable and dirty.
Am I missing something here? Any better way to do this?
Dim tx As String
If Not Me.flProcDONE And Not Me.flProcNOK And Not Me.flProcOK Then
tx = ""
Else
tx = "stProc IN (" & IIf(Me.flProcDONE, kStProcUPD, "99") _
& "," & IIf(Me.flProcNOK, kStProcNOK, "99") _
& "," & IIf(Me.flProcOK, kStProcOK, "99") & ")"
End If
With Me.sfrApply.Form
.Filter = tx
.FilterOn = True
End With
(Partly-working) brute force code:
With Me.sfrApply.Form
If .Filter = "" Then
.Filter = tx
Else
If tx = "" Then
lnStart = InStr(1, .Filter, "AND stProc IN (", vbTextCompare)
If lnStart > 0 Then
lnEnd = InStr(lnStart, .Filter, ")", vbTextCompare)
.Filter = Left(.Filter, lnStart - 1) & Mid(.Filter, lnEnd + 1)
End If
Else
lnStart = InStr(1, .Filter, "stProc", vbTextCompare)
If lnStart > 0 Then
lnEnd = InStr(lnStart, .Filter, ")", vbTextCompare)
.Filter = Left(.Filter, lnStart - 1) & tx & Mid(.Filter, lnEnd + 1)
Else
.Filter = "(" & .Filter & ") AND (" & tx & ")"
End If
End If
End If
.FilterOn = True
End With
It has a few errors, misses some parentheses. Making it work would require an additional 4-5 IFs and many more Instrs. Disgusting. Access filtering keeps adding [] and () to the filter text, that is what makes it near impossible to manipulate from the code.
A few examples of .Form.Filter texts:
"" - no filter
"stProc IN (99,99,1)" - the one I'm trying to manipulate
"([scrCarrierInvoiceGLSQuote].[ctParcelQUOTE] In (1,2))"
"((([stProc] In (99,99,1)))) AND ([scrCarrierInvoiceGLSQuote].[ctParcelSI]=1)"
"(([scrCarrierInvoiceGLSQuote].[ctParcelQUOTE] In (1,2))) AND (stProc IN (99,99,1))"
"((([scrCarrierInvoiceGLSQuote].[ctParcelQUOTE] In (1,2)) AND ([stProc] In (99,99,1)))) AND ([scrCarrierInvoiceGLSQuote].[lsError] Like "COD?")"
I would try something like
With Me.sfrApply.Form
.Filter = .Filter & " AND " & tx
.FilterOn = True
End With
This is just a quick sample but you can elaborate on that.
Well, I did manage to solve it. There was no nice way I have found. Basically:
If the filter string is empty, just add my filter string to it
If the filter part in question is not already in the filter string, just Concatenate it to the end of it (as #iDevlop suggested)
If the filter I'm about to apply is already part of the filter, just change the "IN(...)" part of it - never attempt to remove it.
Here is the code:
Dim txFullFilter As String
Dim txFilterPart As String
Dim lnStProcPos As Long 'Position of the column name in the existing filter text
Dim lnOpenParPos As Long 'Position of the opening parentheses "(" after column name
Dim lnCloseParPos As Long 'Position of the closing parentheses ")" after the opening one
'Create the actual filter text form the column I'm trying to filter from outside.
If Not Me.flProcDONE And Not Me.flProcNOK And Not Me.flProcOK Then
txFilterPart = "0,1,3,7"
Else
txFilterPart = IIf(Me.flProcDONE, kStProcUPD, "99") _
& "," & IIf(Me.flProcNOK, kStProcNOK, "99") _
& "," & IIf(Me.flProcOK, kStProcOK, "99")
End If
txFullFilter = "stProc IN (" & txFilterPart & ")"
'Apply said filter to the subform
With Me.sfrApply.Form
If .Filter = "" Then
.Filter = txFullFilter
ElseIf InStr(.Filter, "stProc") > 0 Then
lnStProcPos = InStr(.Filter, "stProc")
lnOpenParPos = InStr(lnStProcPos, .Filter, "(")
lnCloseParPos = InStr(lnOpenParPos, .Filter, ")")
.Filter = Left(.Filter, lnOpenParPos) & txFilterPart & Mid(.Filter, lnCloseParPos)
Else
.Filter = .Filter & "AND " & txFullFilter
End If
.FilterOn = True
End With

VB.NET DataGridView -> Add a column and fill cells with data from another cell

I have a DataGridView which I fill in the following way:
Function updategrid(ByVal CurrentBroker, ByVal CurrentPallet)
Dim i = 0
Dim SQLText As String
SQLText = "SELECT " & _
"invoer.idInvoer," & _
"invoer.BRKNo as Broker," & _
"invoer.Palletno as Pallet," & _
"invoer.Tabblad AS ValTabblad," & _
"Tabblad.TabbladType, " & _
"invoer.Hardwaretype as Hardwaretype," & _
"invoer.Fabrikant AS Fabrikant," & _
"invoer.Model As Model, " & _
"invoer.Serienummer, " & _
"invoer.AssetTag, " & _
"invoer.Schade, " & _
"invoer.Opmerkingen, " & _
"Tabblad.idTabblad, " & _
"invoer.details, " & _
"invoer.aantal, " & _
"invoer.RegisterDate " & _
"FROM Invoer as invoer " & _
"INNER JOIN Tabblad as Tabblad ON invoer.Tabblad = tabblad.idTabblad " & _
"WHERE invoer.BRKNo = '" & CurrentBroker & "'" & _
"AND invoer.PalletNo = " & CurrentPallet & " " & _
"ORDER BY invoer.RegisterDate DESC"
ds.Tables.Clear()
Try
Data = New DataTable
dataAdap = New MySqlDataAdapter(SQLText, dbconn)
cmdBuilder = New MySqlCommandBuilder(dataAdap)
dataAdap.Fill(ds, "Overzicht")
With DGVOverzicht
.DataSource = ds.Tables("Overzicht")
.ColumnHeadersVisible = True
.AutoGenerateColumns = True
.Visible = True
For i = 0 To .ColumnCount - 1 Step 1
Select Case .Columns(i).HeaderText
Case "ValTabblad" : .Columns(i).Visible = False
Case "details"
' hide the ID's, display text..
.Columns(i).Visible = False
Case "idTabblad" : .Columns(i).Visible = False
Case "idHWModel" : .Columns(i).Visible = False
Case "idHardware" : .Columns(i).Visible = False
Case "idInvoer" : .Columns(i).Visible = False
End Select
Next
End With
Return True
Catch ex As MySqlException
MsgBox(ex.Message)
Return False
End Try
Return 1
End Function
This works like a charm, but I need to add a column next to the column which I hide: Details.
The Column Details holds the ID's of all details selected in the record (e.g. 1;5;2;19;20;100)
I want to give the user a bit more information about the actual selected details, e.g. by do a lookup in MySQL to see what details those ID's actually are..
However, I was told not to do a inner join on a comma separated list in MySQL due to the fact that it would be 'slow as frozen snail'..
So the next thing I can think of is filling the DGV with all the information from the Database, hide the ID column and 'find and replace' the values in another cell, and thus looking it up in MySQL..
If anyone could point me into some kind of direction in this, I'd be very grateful.. since I'm staring at the code for over a few hours now, and not a single lightbulbs seems to pop up :)
Thank in advance!
Well, i think I kind of did it finally.
What I did was select an extra column in the SQLstatemenr (invoer.idInvoer AS details_explain) so that I have an extra column to play with and at the position where I want it.
Then i added the following code to change the content of that column:
' replace ID's in Details_Explain with ShortCodes from DGV
For row = 0 To DGVOverzicht.Rows.Count - 1
Dim strDetailsExplain = ""
Dim DetailIDs As Array
DetailIDs = Split(DGVOverzicht.Rows(row).Cells("details").Value.ToString, ";")
For Each item In DetailIDs
If Not item = Nothing Then
strDetailsExplain = strDetailsExplain & DGV.Rows(Int(item)).Cells("DetailsCode").Value & ":"
End If
Next
DGVOverzicht.Rows(row).Cells("Details_Explain").Value = strDetailsExplain
Next
I suppose this is not the most clean way to do it, but it will have to do, since I don't know a better way of doing it..

DSum function in vba

I want to display sum of a column in a textbox when I hit a button. But it is giving me a compile error: "Wrong number of arguements or invalid property assignment"
The below code is implemented in vba.
Here is the code that I used:
Text19 = Nz(DSum("Total_Units", "6_Provincial_SUB", , "[BudgetYear] =" & [Combo5] & " And [Program_Name] ='" & Replace([Combo7], "'", "''") & "'"), 0)
DSum has three parameters. You have four. Drop the extra comma
Text19 = Nz(
DSum(
"Total_Units",
"6_Provincial_SUB", <==== Here I dropped a comma (,)
"[BudgetYear] =" & [Combo5] & " And [Program_Name] ='" &
Replace([Combo7], "'", "''") & "'"
),
0
)
When things like this happen, I try to find the problem by indenting the expression like above in order to find matching braces etc. Without line continuation character "_" this will not work of cause, but it gives you an idea of the structure of the expression.
I have these functions in my library. They help me in the creation of SQL strings
Public Function SqlStr(ByVal s As String) As String
'Input: s="" Returns: NULL
'Input: s="abc" Returns: 'abc'
'Input: s="x'y" Returns: 'x''y'
If s = "" Then
SqlStr = "NULL"
Else
SqlStr = "'" & Replace(s, "'", "''") & "'"
End If
End Function
Function Build(ByVal s As String, ParamArray args()) As String
'Build("FirstName = {0}, LastName = {1}","John","Doe") -->
'"FirstName = John, LastName = Doe".
'"\n" is expanded with vbCrLf.
Dim i As Long
s = Replace(s, "\n", vbCrLf)
For i = 0 To UBound(args)
s = Replace(s, "{" & i & "}", Nz(args(i)))
Next i
Build = s
End Function
By using them, your SQL would be constructed like this
sql = Build("[BudgetYear] = {0} AND [Program_Name] = {1}", _
Combo5, SqlStr(Combo7))
You have a comma too many after the domain parameter:
"6_Provincial_SUB", ,
That would make the space the criteria parameter, and the actual criteria an unknown fourth parameter.

Textbox "Like" Search

I have a form that I want to search for anything containing what is entered into a textbox. Right now the search only picks up data that matches exactly (ie MDD), but I want it to capture anything containing the searched item automatically (ie *MDD*)
Ideally I would like a user to enter what they are searching for and get anything that contains that search.
The code I wrote (that partially works) is:
`
If Me.tbIni = "" Or IsNull(Me.tbIni) Then
stCriteria = ""
Else
If InStr(1, Me.tbIni, "LIKE ") Then
stCriteria = "CURQCDB.DT_ini '" & Me.tbIni & "'"
Else
stCriteria = "CURQCDB.DT_ini = '" & Me.tbIni & "'"
Help would be much appreciated.
Just search for *MDD* instead of MDD
Try the following instead. I also took the liberty of sanitizing the input a bit so that it properly handles double and single quotes:
If Me.tbIni = "" Or IsNull(Me.tbIni) Then
stCriteria = ""
Else
stCriteria = "CURQCDB.DT_ini LIKE ""*" & Replace(Me.tbIni, """", """""") & "*"""
End If