Convert values from Access Combo Box to string - ms-access

I need to convert the values from combo box to a string so I can add that string to a variable, to a function to eventually add to a database.
Here is my sub that grabs text from my form and from the combo box:
Private Sub cbRowStudentGrade_Change()
Course_ID.SetFocus
rowCourseID = Course_ID.Text
StuRed_ID.SetFocus
rowStudentRedID = StuRed_ID.Text
cbRowStudentGrade.SetFocus
cbRowStudentGrade = cbRowStudentGrade.Column(0)
CurrentDb.Execute "qryInputGrades"
MsgBox (rowCourseID)
MsgBox (rowStudentRedID)
MsgBox (cbRowStudentGrade)
Requery
Repaint
End Sub
And here are the functions that I am using as criteria in my Access query builder.
Public Function funcRowCourseID() As String
funcRowCourseID = rowCourseID
End Function
Public Function funcRowStudentRedID() As String
funcRowStudentRedID = rowStudentRedID
End Function
Public Function funcCbRowStudentGrade() As String
funcCbRowStudentGrade = cbRowStudentGrade
End Function
My query:
INSERT INTO tblRegistrationGrade ( Red_ID, Course_ID, Grade )
VALUES (funcRowStudentRedID(), funcRowCourseID(), funcCbRowStudentGrade());
I think there is a datatype mismatch between the database and what the combobox value actually is. But, if there were a datatype mismatch wouldn't there be an error stating as such? My database requires short text, which these are.

You're doing this way too complicated.
You can directly refer from your INSERT query to the form controls:
INSERT INTO tblRegistrationGrade ( Red_ID, Course_ID, Grade )
VALUES (Forms!myForm!StuRed_ID, Forms!myForm!Course_ID, Forms!myForm!cbRowStudentGrade);
and get rid of almost all of the code, except calling the query.
As John wrote, it would be better placed in cbRowStudentGrade_AfterUpdate().
Or, instead of the INSERT query, use a DAO.Recordset with AddNew, see:
How to: Add a Record to a DAO Recordset
There you can also directly use the values from your from controls without worrying about data types.

Related

OnlyDigits Function in Criteria

My database has a job number field that consists of year+month/serialnumber+type. There can be multiple jobs with the same job number:
201812/6Door
201812/6Stair
201812/6Wardrobe
When the user wants to change the date of any/all of these records I want all 201812/6 jobs to show in a form.
I have successfully used the OnlyDigits function below to pull only numbers from the text field: OnlyDigits(JobNumber) = 2018126. But I can't figure out how to filter the form to show all jobs containing 2018126.
I have tried using this query but get an error saying expression typed incorrectly or is too complex.
SELECT onlydigits(jobnumber) AS JobNumberDigits, tbldelivery.DelDateDoors, tbldelivery.Lag, tbldelivery.ProductionDate, tbldelivery.OrderNumber, tbldelivery.JobNumber
FROM tbldelivery
WHERE (((onlydigits(jobnumber))=OnlyDigits([Forms]![tblDelivery]![JobNumber])));
I also tried using a where expression in Docmd.OpenForm but that didn't work either. Can anyone suggest how I use this function to filter?
Public Function OnlyDigits(ByVal pInput As String) As String
Static objRegExp As Object
If objRegExp Is Nothing Then
Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
.Global = True
.Pattern = "[^\d]"
End With
End If
OnlyDigits = objRegExp.Replace(pInput, vbNullString)
End Function
Try specifying the parameter to free Access from guessing:
PARAMETERS
[Forms]![tblDelivery]![JobNumber] Text;
SELECT
OnlyDigits(JobNumber) AS JobNumberDigits,
tbldelivery.DelDateDoors,
tbldelivery.Lag,
tbldelivery.ProductionDate,
tbldelivery.OrderNumber,
tbldelivery.JobNumber
FROM
tbldelivery
WHERE
OnlyDigits(JobNumber)=OnlyDigits([Forms]![tblDelivery]![JobNumber]);
or:
WHERE
OnlyDigits(CStr(Nz(JobNumber, 0)))=OnlyDigits([Forms]![tblDelivery]![JobNumber]);

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.

SSIS Convert Blank or other values to Zeros

After applying the unpivot procedure, I have an Amount column that has blanks and other characters ( like "-"). I would like to convert those non-numberic values to zero. I use replace procedure but it only converts one at the time.
Also, I tried to use the following script
/**
Public Overrides Sub Input()_ProcessInputRows(ByVal Row As Input()Buffer)
If Row.ColumnName_IsNull = False Or Row.ColumnName = "" Then
Dim pattern As String = String.Empty
Dim r As Regex = Nothing
pattern = "[^0-9]"
r = New Regex(pattern, RegexOptions.Compiled)
Row.ColumnName = Regex.Replace(Row.ColumnName, pattern, "")
End If
End Sub
**/
but i'm getting error.I don't much about script so maybe I placed in the wrong place. The bottom line is that I need to convert those non-numberic values.
Thank you in advance for your help.
I generally look at regular expressions as a great way to introduce another problem into an existing one.
What I did to simulate your problem was to write a select statement that added 5 rows. 2 with valid numbers, the rest were an empty string, string with spaces and one with a hyphen.
I then wired it up to a Script Component and set the column as read/write
The script I used is as follows. I verified there was a value there and if so, I attempted to convert the value to an integer. If that failed, then I assigned it zero. VB is not my strong suit so if this could have been done more elegantly, please edit my script.
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
' Ensure we have data to work with
If Not Row.ColumnName_IsNull Then
' Test whether it's a number or not
' TryCast doesn't work with value types so I'm going the lazy route
Try
' Cast to an integer and then back to string because
' my vb is weak
Row.ColumnName = CStr(CType(Row.ColumnName, Integer))
Catch ex As Exception
Row.ColumnName = 0
End Try
End If
End Sub

MS ACCESS Retrieving "Table Description" Through Query

I've been looking everywhere for a way of accessing a table's description (same one that appears when you right click a table>table properties) through a SELECT query.
I tried using MSysObjects but I can only retrieve the name of the table using that.
Is it possible to do this through a query or is VBA needed?
As Remou says, you can't get it from a query (but you can include a function that returns it in a query). Here's another function:
Public Function GetTableDescr(stTableName As String) As String
On Error Resume Next
GetTableDescr = CurrentDb.TableDefs(stTableName).Properties("Description").Value
End Function
Here's a query that returns all the non-system tables, with their dates and descriptions (using the function above):
SELECT MSysObjects.Name, msysobjects.datecreate, msysobjects.dateupdate, GetTableDescr([Name]) AS Description
FROM MSysObjects
WHERE (((MSysObjects.Name) Not Like "~*") AND((MSysObjects.Name) Not Like "MSys*") and ((MSysObjects.Type)=1));
Finally, you can do an almost identical function for queries. The trick I found is that you only return non-inherited descriptions, otherwise if a query has no description you get the description of the queried object:
Public Function GetQueryDescr(stQryName As String) As String
On Error Resume Next
If CurrentDb.QueryDefs(stQryName).Properties("Description").Inherited = False Then
GetQueryDescr = CurrentDb.QueryDefs(stQryName).Properties("Description").Value
End If
End Function
The On Error Resume Next is necessary, because until the object has a description the property is null.
You can get the description from the table schema or from TableDef properties, but I do not think a standard query will work.
Set rs = CurrentProject.Connection.OpenSchema(adSchemaTables, _
Array(Empty, Empty, "Rules", Empty))
Debug.Print rs!Description
Using GetQueryDescr() above, you can run this query against the hidden sys table
SELECT MSysObjects.Name, GetQueryDescr([Name]) AS Properties, MSysObjects.DateCreate, MSysObjects.DateUpdate
FROM MSysObjects
WHERE (((MSysObjects.Name) Not Like "~sq_*") AND ((MSysObjects.Type)=5));
type 5 is for queries

Populating multiple values in rdlc reporting

I am using rdlc report, i have a column in database which i want to display in the report.
vehicleDamageArea=1,2,3
In the report I need to mark the placeholders with these values.
=iif((Fields!vehicleDamageArea.Value="3"),Chr(253),Chr(168)) like this.
But as we know,it will check the whole value 1,2,3="3" not the splitted values.
Any suggestion to check by splitting the vehicleDamageArea parameter.
I made it to work as below
Public Shared Function CheckValue(ByVal InString As String,ByVal input as String) As Char
Dim output As String = String.Empty
Dim Parts() As String = InString.ToString().Split(",")
For i As Integer = 0 To Parts.Length - 1
If Parts(i) = input Then
output = Chr(0120)
Exit For
Else
output = Chr(0111)
End If
Next i
Return output
End Function
You can get the individual values using the split function in reporting services. It returns a zero-based string array, so for your example you need
=Split(First(Fields!ID.Value),",")(2)
You should make a function that accept a comma separated expression, than process this string and return a Boolean, then call this function as for boolean value.