Passing html Table data in viewbag - html

I have some data in a database which I am collection in a control function. I place the returned values in to a structure as below:
For I As Integer = 1 To Mygallery.Length - 1
With Mygallery(I)
Select Case True
Case Counter = 0
Line &= "<tr><td><img src=""" & .GalleryImage.ToString & """ alt=""" & .Customer_Name.ToString & """ height=""200"" width=""150""/> </td>"
Counter = 1
Case Counter = 1
Line &= "<td><img src=""" & .GalleryImage.ToString & """ alt=""" & .Customer_Name.ToString & """ height=""200"" width=""150""/> </td></tr>"
Counter = 0
Case Else
End Select
End With
Next
ViewBag.galleryLine = Line
As you can see I add this to a ViewBag.galleryLine item. In my html view page I then reference this table style="width:100%">#ViewData("Table") table
(Had to remove the html code as I could not get it to show)
Anyway the problem is it does not write it as a table but as text!
if I put the text that the viewbag returns straight in to the view it displays correctly. So it not a formatting issue!

Use the Html.Raw method, like this
#Html.Raw(ViewData("Table"))

Related

MS Access VBA - Append String to Text Box, Reappend when updated

I am trying to creating a MS Access form, where a user would input comments in a text box (Input), and the text would be appended each time to a summary box which would be greyed out and is non editable.
The Input comments will be auto formatted to include the date timestamp and username using the following code:
Private Sub IncidentDescriptionInput_AfterUpdate()
Dim Output As String
Output = Me!IncidentDescriptionInput.Value
Output = Output & " " & Format(Now(), "dd-mmm-yy") & "/" & Format(Now(), "hh:nn") & "/" & Environ("UserName") & ";"
Me!IncidentDescriptionInput.Value = Output
End Sub
Your current code already demonstrates the techniques required to achieve your goal:
Updating the value held by a form control as part of the event handler for the AfterUpdate event. You already do this in your existing code with Me!IncidentDescriptionInput.Value = Output
Retrieving the existing value of a form control for concatenation with another string. You already do this in your existing code with Output = Me!IncidentDescriptionInput.Value
Concatenating the existing value of a form control with another string. You already do this in your existing code with Output = Output & " " & Format(Now(), "dd-mmm-yy") ...
And so, you already have the building blocks required to achieve the desired result.
Assuming that the summary box is called IncidentDescriptionSummary, then the code might be modified to something like:
Private Sub IncidentDescriptionInput_AfterUpdate()
Dim Output As String
Output = IncidentDescriptionInput & " " & Format(Now(), "dd-mmm-yy\/hh:nn\/") & Environ("UserName") & ";"
IncidentDescriptionInput = Output
IncidentDescriptionSummary = IncidentDescriptionSummary & vbCrLf & Output
End Sub

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

Concatenating Null values in DCount query that compare to corresponding concatenated table fields

Using Access 2007.
I have to compare several data entry fields with their corresponding tables fields. If the fields match, do not add a new record. If not, add a new record with the values.
AnimalInfo Table fields
WHno (Wildlife Health Number)
Species
LETClr1 (Left Ear Tag Color 1)
LETNo1 (Left Ear Tag Number 1)
LETClr2 (Left Ear Tag Color 2)
LETNo2 (Left Ear Tag Number 2)
RETClr1 (Right Ear Tag Color 1)
RETNo1 (Right Ear Tag Number 1)
RETClr2 (Right Ear Tag Color 2)
RETNo2 (Right Ear Tag Number 2)
Form F_HotelForm unbound fields
txtSpecies
txtLETClr1
txtLETNo1
txtLETClr2
txtLETNo2
txtRETClr1
txtRETNo1
txtRETClr2
txtRETNo2
I am trying to create a DCount to check and see if there are any matching records. The animal's uniqueness is determined by its species and ear tag information. It can have one ear tag number and color, or four. (Some of the older data has none but I can't do anything about that! In those cases, a new record, i.e. new Wildlife Health Number will be generated)
This is what I want to accomplish with this form:
If there are no matching fields (DCount = 0) add a new record and update fields from the form into table.
If there is 1 matching record, then the animal's wildlife health number is displayed (in a new form eventually)
If there are multiple records, then these are displayed in another form and the user needs to pick the correct animal.
LETClr1 and LETNo1 are paired.
LETClr2 and LETNo2 are paired.
RETClr1 and RETNo1 are paired.
RETClr2 and RETNo2 are paired.
Any and all of these fields could have values or not. Left ear tag numbers and colors could have been entered as either LETClr1 or LETClr2 so I have to compare both LETClr1 and LETClr2 with the txtLETClr1 data entry. (This holds true for all paired fields)
Below is a sample of the script so far. It is very rudimentary as I am very new to this and am just trying to see what works.
Private Sub GenerateWHno_Click()
Dim rs As DAO.Recordset
If IsNull(Forms!F_HotelEntry!txtSpecies) Or (Forms!F_HotelEntry!txtSpecies) = "" Then
MsgBox "Species is a required field. Please enter a species"
Exit Sub
End If
MsgBox txtSpecies
SpeciesCount = DCount("[Species]", "AnimalInfo", "[Species]= '" & txtSpecies & "'AND [L_ET_Color1]='" & txtL_ET_Color1 & "' AND [L_ET_No1]='" & txtL_ET_No1 & "'")
If SpeciesCount > 1 Then
MsgBox SpeciesCount & " Greater than 1"
ElseIf SpeciesCount = 0 Then
MsgBox "You need a new WHno"
WHno = Nz(DMax("WHno", "AnimalInfo")) + 1
MsgBox WHno
Set rs = CurrentDb.OpenRecordset("AnimalInfo")
rs.AddNew
rs!WHno = WHno
rs!Species = txtSpecies
rs!L_ET_Color1 = txtL_ET_Color1
rs!L_ET_No1 = txtL_ET_No1
rs!R_ET_Color2 = txtR_ET_Color2
rs.Update
rs.Close
Else
End If
Forms!F_HotelEntry!txtSpecies = ""
Forms!F_HotelEntry!txtL_ET_Color1 = ""
Forms!F_HotelEntry!txtL_ET_No1 = ""
End Sub
So the problem is that I cannot concatenate NULL fields. The DCount only works if there is Non Null data in the form/table.
Any ideas as to how I can work around this?
Many thanks.
My comments are getting garbled so I am putting below original posting.
I copied the suggested code into module and rewrote query a couple of different way but still got error message: Run-time error 424. Object required
SpeciesCount = DCount("[Species]", "AnimalInfo", "[Species] = txtSpecies AND (is_null([L_ET_Color1],"""") = is_null(txtL_ET_Color1,""""))")
SpeciesCount = DCount("[Species]", "AnimalInfo", "[Species] = '" & txtSpecies & "'AND is_null([L_ET_Color1],"") ='" & is_null(txtL_ET_Color1, "") & "' AND [L_ET_No1]='" & txtL_ET_No1 & "'")
I have been tinkering with this for 3 hours and am no closer to a solution. What am I doing wrong?
In a module create the following function:
public function is_null(ctl as variant, nullreplace as string) as variant
if ctl is null then
is_null = nullreplace
else
is_null = ctl
end if
end function
In your query change your reference to a given field, yourField to is_null(yourField, "").
In a string enclosed in double quotes, this will need to be is_null(yourField, """") (it needs a double-quote to escape a double-quote, so 4 is 2 internally)

Checkbox named by string and variable. Trouble getting VBScript to recognize it as an object

Modified Code.
Error message: Object required:'document.getElementsByName(...)(...)'
Line:59
Char:13
Code with Error: if document.getElementsByName("chk" & arrName(ii))(0).Checked then
<SCRIPT language="VBScript">
Dim arrName(),arrExe()
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile("software/software.txt")
strSoftware = objFile.ReadAll
objFile.Close
arrSplit = Split(strSoftware, vbNewLine)
i = 0
For Each strLine in arrSplit
ReDim Preserve arrName(i + 1)
ReDim Preserve arrExe(i + 1)
arrName(i) = TRIM(Left(strLine,InStr(strLine,";")-1))
arrExe(i) = TRIM(Right(strLine,InStr(strLine,";")))
strHTML = CheckboxArea.InnerHTML
strHTML = strHTML & "<BR><INPUT TYPE='CHECKBOX' VALUE='" & arrName(i) & "' NAME ='chk'" & arrName(i) & " />" & arrName(i)
CheckboxArea.InnerHTML = strHTML
i = i + 1
Next
Sub Confirm
Dim objForm, Element
set objForm=document.forms("SoftwareSelect")
set Element=objForm.elements
i=0
ii=0
for i=0 to Element.length
if Element(i).type="checkbox" then
if document.getElementsByName("chk" & arrName(ii))(0).Checked then
MsgBox(arrName(ii) & " is checked.")
end if
ii = ii + 1
end if
next
End Sub
</SCRIPT>
Your code cannot work. You say that arrName is an array of strings. A string is not a checkbox object. The solution is so near though.
Just look a bit above the code line. This should work, shouldn't it?
if Element(i).type="checkbox" then
ii = ii + 1
if "chk" & Element(i).Checked then
MsgBox(arrName(ii) & " is checked.")
end if
end if
An expression like this:
"chk" & arrName(ii).Checked
will first try to get the value of the Checked property of an object stored in field ii of array arrName before concatenating that value to the string "chk". What you actually mean to do is get the element with the name "chk" & arrName(ii) and then get the value of the Checked property of that element. Try this:
If document.getElementsByName("chk" & arrName(ii))(0).Checked Then
...
"chk" & arrName(ii) is the value of the name attribute identifying the element you want to obtain.
document.getElementsByName(...) returns a collection of all elements whose name attribute has that value.
...(0).Checked selects the first element from that collection and returns the value of its Checked property.
As a side note: you may want to use the id attribute instead of the name attribute for identifying elements in a page, because that attribute by definition must be unique in a page. That way you could directly retrieve a particular element via getElementById() and won't have to deal with fiddling it out of a collection first.
Edit: If you want to iterate over all checkboxes in the page, something like this might be more suitable:
For Each tag In document.getElementsByTagName("input")
If LCase(tag.Type) = "checkbox" Then
'do stuff
End If
Next

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