What is the VBA code to parse a value into a different field in the db when a checkbox is clicked?
Example code (that doesn't work):
Private Sub Step_1_Click()
If Step_1.Value = True Then
Step1_score.Value = 10
End if
End Sub
I removed the "value" and got no code errors but the 10 is not showing up in the Step1_score variable in the db. Any advise on the VBA code to parse a value from a checkmark? Thanks!
You probably want to use the _AfterUpdate event to make sure the value of Step_1 gets changed before the event fires:
Private Sub Step_1_AfterUpdate()
If Step_1.Value = True Then
Step1_score.Value = 10
End if
End Sub
Related
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]);
I'm trying to add a record to a ComboxBox source when the entered item isn't in the list.
The code correctly creates the record and then returns Response = acDataErrAdded but still displays the error Item not in list. And if I then manually scroll down the list the new user record is there!
tblUsers
ID(Auto Number), UserCode(Text), UserName(Text)
1, ID00001, "Tom Jones"
2, ID00007, "Fred Smith"
cmbUsers
RowSource = SELECT UserCode, UserName, ID FROM tblUsers
Bound Column = 3
Column Count = 3
Column Widths = "2cm;4cm;2cm"
Input Mask "ID00000;;_"
Private Sub cmbUsers_NotInList(NewData as string, Response as integer)
dim sUserCode as string
sUserCode = "ID" & NewData ' Doesn't have the "ID" ?!?!?
if ActiveDirectoryUserIDExists(sUserCode) then
AddNewUserFromActiveDirectory sUserCode
Response = acDataErrAdded
exit sub
end if
Response = acDataErrDisplay
End Sub
When NotInList is entered the NewData value is 00001 and is not prepended with the input mask "ID".
So I suspect that when the NotInList sub returns Access is using NewData value that is not prepended with "ID" so never finds the value is the re-queried list!
I have tried setting the NewData value to "ID" & NewData before exiting but this did not work.
So I'm really not sure!
Any ideas on how I can get this to work?
InputMask details
The docs for InputMask property say this regarding the second section for InputMask property:
If you use 0 for this section, all literal display characters (for example, the parentheses in a phone number input mask) are stored with the value; if you enter 1 or leave this section blank, only characters typed into the control are stored.
Example:
Input Mask: "ID"00000;0;_
This should cause the entire text value, e.g. "ID00001", to be passed to NotInList event handler in the NewData parameter.
Buggy InputMask behavior for bound ComboBox control
After extensive testing, it seems that this only works flawlessly for an unbound control. If the datatype of the column in the Control Source property does not support saving the fully formatted text value, then the text input value will be formatted to be compatible with (i.e. match) the bound field datatype even though the displayed text is not what is actually saved in the field. This is the best description I could come up with and I could not find any direct resolution to this bug.
So if the bound column is of text type (e.g. Short Text) then there is no problem. But as in the original question where the bound field is a foreign key column of Long Integer type (Control Source: [Assigned User]; Bound Column: 3; Row Source has [ID] as third column), then the input value is converted to a numerical string value before being passed to NotInList.
Workaround
This preserves the desired behavior of the ComboBox and InputMask with minimal code and one extra hidden TextBox control.
Add a simple bound TextBox control for the numeric foreign key field with the following properties:
Control Name: [Foreign_Key_Column_Name]
Control Source: [Foreign_Key_Column_Name]
Visible: False
Add unbound ComboBox control setup with InputMask property and NotInList event handler:
Control Name: [X_Foreign_Key_Column_Name]
Control Source: < Blank / null >
InputMask: "ID"00000;0;_
Row Source: SELECT UserCode, UserName, ID FROM tblUsers
Bound Column: 3 (this is not the same as the Control Source property binding to the form record source and apparently not what causes the buggy behavior)
Add NotInList event handler to unbound control with same code as before.
Private Sub X_Foreign_Key_Column_Name_NotInList(NewData as string, Response as integer)
dim sUserCode as string
sUserCode = NewData
if ActiveDirectoryUserIDExists(sUserCode) then
AddNewUserFromActiveDirectory sUserCode
Response = acDataErrAdded
Else
Response = acDataErrDisplay
end if
End Sub
Add the following code to manually bind the ComboBox value to the numeric foreign-key column.
Private Sub Form_Current()
Me.X_Foreign_Key_Column_Name.Value = Me.Foreign_Key_Column_Name.Value
End Sub
Private Sub X_Foreign_Key_Column_Name_AfterUpdate()
Me.Foreign_Key_Column_Name.Value = Me.X_Foreign_Key_Column_Name.Value
End Sub
I need to get the ID parameter in a URL, for example I have
http://apps/inventory/others.aspx?ID=8678
How do I extract the 8678, I've looked at the method of the Object WinHttp.WinHttpRequest.5.1 but I haven't found anything. Could that be possible with a simple substring? The URL is always the same and there is always one GET parameter,
Thanks
Try like this:
Option Explicit
Public Sub TestMe()
Debug.Print ExtractAfter("http://apps/inventory/others.aspx?ID=8678", "ID=")
Debug.Print ExtractAfter("http://apps/inventory/others.aspx?ID=867843", "ID=")
End Sub
Public Function ExtractAfter(strInput As String, strAfter As String) As String
ExtractAfter = Mid(strInput, InStr(strInput, strAfter) + Len(strAfter))
End Function
This is what you get in the immediate window:
8678
867843
In VBA, assuming you have the url in a variable url:
Debug.Print Mid(url, InStr(url, "ID=") + 3)
However, works only correct if the ID parameter is always present and always the only paramter, else you need some more sophisticated string handling.
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.
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