For my form, almost all paper forms should come with an ID that you enter into one field. However, a few forms will come with no IDs, and I want to generate into a different field an ID number based on the user entering the information.
What I want it to do is to create a 4-digit number, where the first digit is from the field usernum, and the last 3 digits are just sequentially counting.
The first time they press the button, nothing is in the usernum field yet, and I think this may be the cause of my runtime 94 error?
below is my code and the error line
Private Sub Command100_Click()
'on click of button InsightId is generated
Me.idnum = NewInsightID()
End Sub
Public Function NewInsightID() As Long
Dim lngNextID As Long
'Find highest ID in the test table and add 1
lngNextID = (Me.usernum * 1000) + DMax([idnum], "test", "usernum=" & Me.usernum) + 1
'ABOVE IS THE LINE WITH ERRORS
'Assign function the value of the Next ID
NewInsightID = lngNextID
End Function
I have tried various workarounds but none have worked
below is what i've tried and hasn't worked:
lngNextID = (Me.usernum * 1000) + 1 + NZ(DMax([idnum], "test", "usernum=" & Me.usernum),0)
lngNextID = (Me.usernum * 1000) + 1 + Nz(DMax([idnum], "test", "usernum=" & Nz(Me.usernum, 0)), 0)
lngNextID = (Me.usernum * 1000) + 1 + DMax([idnum], "test", "usernum=" & Nz(Me.usernum, 0))
EDIT 2:
Okay, have made process. Sort of have it working, except for two things--for some reason it won't start at 001, only 002. After that, it does move incrementally by one.
The main thing is an error if usernum is blank. Again, I don't anticipate this ever being the case, but when it comes to data entry I think it's best to assume the dumbest possible users, and since I'm going to be on vacation when this database is used I don't want to get any calls about errors. I'm trying to do it so that if usernum is blank, they get a message that tells them the error and cancels the sub, but for some reason it isn't working, and then later in the code i get the error message Run time error 3075 missing operator in query expression 'usernum='. I assumed doing a message box to fix that would solve the problem, but I can't get the message box to work.
Private Sub Command100_Click()
'on click of button InsightId is generated
If usernum = Null Then
Beep
MsgBox ("you cannot leave usernum blank")
Cancel = True
Else: Me.idnum = NewInsightID()
End If
End Sub
Public Function NewInsightID() As Long
Dim lngNextID As Long
'Find highest ID in the test table and add 1
If DMax("idnum", "test", "usernum=" & Me.usernum) = Null Then
lngNextID = (Me.usernum * 1000 + 1)
Else:
lngNextID = 1 + Nz(DMax("idnum", "test", "usernum=" & Me.usernum), Me.usernum * 1000 + 1)
End If
'Assign function the value of the Next ID
NewInsightID = lngNextID
End Function
EDIT3:
aaah I solved it incredible!!
Private Sub Command100_Click()
'on click of button InsightId is generated
If IsNull(Me.usernum) Then
Beep
MsgBox ("you cannot leave usernum blank")
Cancel = True
Else: Me.idnum = NewInsightID()
End If
End Sub
Public Function NewInsightID() As Long
Dim lngNextID As Long
'Find highest ID in the test table and add 1
If DMax("idnum", "test", "usernum=" & Me.usernum) = Null Then
lngNextID = (Me.usernum * 1000 + 1)
Else:
lngNextID = 1 + Nz(DMax("idnum", "test", "usernum=" & Me.usernum), Me.usernum * 1000)
End If
'Assign function the value of the Next ID
NewInsightID = lngNextID
End Function
This one is close:
lngNextID = (Me.usernum * 1000) + 1 + NZ(DMax([idnum], "test", "usernum=" & Me.usernum), 0)
but the first parameter of DMax (the fieldname or expression) is a string too:
lngNextID = (Me.usernum * 1000) + 1 + NZ(DMax("idnum", "test", "usernum=" & Me.usernum), 0)
Related
I am trying to add some data into 'List' form(exactly copy).
Below is my code.
Option Compare Database
Private Sub addToListSample_Click()
Dim introw As Integer
introw = ListSample.ListIndex + 1
ListSample.Column(0, introw) = TextP11.Value
ListSample.Column(1, introw) = TextP12.Value
ListSample.Column(2, introw) = TextP13.Value
ListSample.Column(3, introw) = TextP14.Value
ListSample.Column(4, introw) = TextP15.Value
ListSample.Column(5, introw) = TextP16.Value
ListSample.Column(6, introw) = TextP17.Value
End Sub
But when I execute it, I get following error.
"Run-time error '424' : Object required"
Why do I get this error? I think this is pretty easy code, but I don't know why this error keep annoying me....
Start by changing
introw = ListSample.ListIndex + 1
to
introw = Me.ListSample.ListIndex + 1
At some point you will probably also have to check that the listbox is actually selected or you will always get 0.
edit:
'add a row with hard-coded values:
'Dim introw As Integer
'introw = Me.lstEmpty.ListIndex + 1 '<-- not really needed when using AddItem.
'Me.lstEmpty.AddItem "value 1;value 2"
'or add a row using values from textboxes:
Me.ListSample.AddItem TextP11.Value & ";" & TextP12.Value & ";" & TextP13.Value
This will add values to one row, in different columns.
Your list box's Row Source Type will have to be set to Value List.
The Column Count will have to match the number of columns you're adding.
I am trying to import data to a table. Basically its a MCQs. All my questions are having superscripts and subscripts, for example X2 , and log52....
I have more than 2000 records, i have to import it. But after importing it comes in plain format, not taking powers. My DB is MYSQL (UTF-8)
Here is the example data
If log5 2, log5 (2x - 5) and log 5(2x - 7/2) are in AP , then x is equal to
after impoting it looks like above, but actually it should be
If log5 2, log5 (2x - 5) and log 5(2x - 7/2) are in AP , then x is equal to
Somebody plz suggest me how to do it
Here's a quick fix for the Subscripts:
Sub log_Script()
Dim cel As Range, rng As Range
Dim i&, k&
Dim myText$, findText$, curStr$
Set cel = Range("A1")
'myText = cel.Value
For i = 1 To Len(cel.Value)
k = k + 1
curStr = Mid(cel.Value, i, 1)
If curStr <> " " Then
findText = findText + curStr
ElseIf curStr = " " Then
findText = ""
End If
Debug.Print findText
If findText = "log" Then
If Mid(cel.Value, i + 1, 1) = " " Then
With cel.Characters(Start:=k + 2, Length:=1).Font
.Subscript = True
End With
Else
With cel.Characters(Start:=k + 1, Length:=1).Font
.Subscript = True
End With
End If
End If
Next i
End Sub
This will go through a range (set currently to be A1:A10), and for each cell, it'll look for log then take the next number and make it subscript. (Note: This is assuming all logs will have base < 10, let me know if that's not necessarily the case).
I could probably make this better, if you can post a few rows or cells from your CSV so I can see what the formatting is exacly like. (Or screenshot a part of your data, that 'd work too).
In one Short Text column of a table such data was stored "any_text_N" where N is some number specific for each row.
I need to replace N by N+1.
Could any one provide query to do it?
Assuming (1) the number is always the rightmost characters, and (2) there is an underscore preceding the number, you can create a Function to parse the number and return the incremented value (see below).
Then to test it, create a query like follows (MAKE SURE YOU TEST FIRST!!!):
SELECT Table2.MyText, resetnbr([MyText]) AS NewVal
FROM Table2
WHERE (((Table2.MyText) Is Not Null));
Then to update your data:
UPDATE Table2 SET Table2.MyText= resetnbr([MyText])
WHERE (((Table2.MyText) Is Not Null));
Public Function ResetNbr(strIn As String) As String
'Assumes: (1) Number in rightmost position of string; (2) underscore preceeds number
Dim iLen As Integer
Dim i As Integer
Dim sNbr As String
If strIn = "" Then
ResetNbr = strIn
Exit Function
End If
iLen = Len(strIn)
For i = iLen To 1 Step -1
If Mid(strIn, i, 1) = "_" Then
Exit For
End If
Next i
If i > 1 Then
sNbr = Mid(strIn, i + 1, 99)
sNbr = sNbr + 1
ResetNbr = left(strIn, i) & sNbr
Else
' No underscore found!
ResetNbr = strIn
End If
End Function
I have been trying to figure this error out for the past few days with no luck. I am hoping one of you would be able to help. I am getting "value used in formula of wrong data type.
Quick explanation:
convert functions like this one to its corresponding text (20054/18393)*100.0
the 5 digit numbers are Field IDs that refer to questions.
ID Question
20054 How many days of year do you work
18393 How many days of vacation do you get a year
The result I am trying to get to is (How many days of year do you work / How many days of vacation do you get a year) *100.0
It could be easily done manually if it was just a hand full. I have over 2600 formulas that need to be converted.
I created this function below which is resulting in the error mentioned in the title. Any assistance would be greatly appreciated
Here is my function
Function Test(sInput As String) As String
Dim i As Long
Dim num As String
Dim Text, a, str, shortname As String
For i = 1 To Len(sInput)
a = Mid(sInput, i, 1)
If IsNumeric(a) Then
num = num & a
Text = ""
Else
If a = "." Then
num = num & a
Else
'search for num value in second sheet short name
shortname = WorksheetFunction.VLookup(WorksheetFunction.Int(num), Worksheets("questionlist").Range("A3:F2537"), 5, False)
num = ""
End If
Text = shortname & a
shortname = ""
End If
str = str & Text
Next
Test = str
End Function
The error is raised because you are passing blank value to INT Function in the line
WorksheetFunction.VLookup(WorksheetFunction.Int(num), Worksheets("questionlist").Range("A3:F2537"), 5, False)
To reproduce the error Type =INT("") in any cell
To fix this handle blank values
Updated Answer:
Function Formula2Text(ByRef myCell As Range) As String
Dim QuestionId As Integer
Dim strInput As String
'Get Formula instead of values
strInput = myCell.FormulaR1C1
'Use Regex to Catch all ID's
Set Regex = CreateObject("VBScript.RegExp")
Set rnglookup = Worksheets("questionlist").Range("A3:F2537")
Regex.Global = True
Regex.Pattern = "\d+"
For Each Match In Regex.Execute(strInput)
'Skip if the ID is 100
If (Match.Value <> 100) Then QuestionId = Match.Value
'Lookup ID in the rnglookup,Make sure the Ids are sorted in asc in the questionlist sheet
Qntxt = Application.VLookup(QuestionId, rnglookup, 5, False)
If IsError(Qntxt) Then Qntxt = "Missing Lookup"
'Replace the ID with the lookup
strInput = Replace(strInput, QuestionId, Qntxt)
Next
Formula2Text = strInput
End Function
Usage:In the cell next to the formula use the function by referencing the formula
=Formula2Text(A1)
I am trying to iterate through a record-set twice. Once to write all of the non-zero results, followed by a second run through to write all the rows that have a zero in a particular column so that all of those rows with the value of 0 are at the end of the file. However since .EOF has been triggered with the first run through it is still "True" when I try to run through it again. What is the best way to run through it twice?
With CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
Do Until .EOF
If .Fields(2) = 0 Then
Else
strCSV = ""
For x = 0 To .Fields.Count - 1
'No Qualifier
strCSV = strCSV & strDelimiter & Nz(.Fields(x), vbNullString)
Next x
'Eliminate Back to back strQualifiers or Qualifiers if changed
strCSV = Replace(strCSV, strQualifier & strQualifier, "")
strPrint = Mid(strCSV, Len(strDelimiter) + 1)
Print #intOpenFile, strPrint
End If
.MoveNext
Loop
Do Until .EOF
If Nz(.Fields(2), vbNullString) = 0 Then
strCSV = ""
For x = 0 To .Fields.Count - 1
'No Qualifier
strCSV = strCSV & strDelimiter & Nz(.Fields(x), vbNullString)
Next x
'Eliminate Back to back strQualifiers or Qualifiers if changed
strCSV = Replace(strCSV, strQualifier & strQualifier, "")
strPrint = Mid(strCSV, Len(strDelimiter) + 1)
Print #intOpenFile, strPrint
End If
.MoveNext
Loop
End With
To answer your question, just used .MoveFirst in between your two runs. But #Remou makes a good point that your can avoid this complication just by adding an ORDER BY into your SQL (+1)
Very old question but appears to have been active recently, and it's still coming up in searches, which is how I ended up here. And, as a new member, I need to start somewhere, so be kind.
The OP appears to be trying to code round a particular requirement, which is to have all the results in ascending numeric order, but with the zero items at the bottom. You can do this in SQL in one statement like this:
SELECT * FROM MyTable
ORDER BY MyNumField DESC, MyOtherSortField
WHERE MyNumField > 0
UNION ALL
SELECT * FROM MyTable
ORDER BY MyOtherSortField
WHERE MyNumField = 0
A UNION merges the results of two or more SELECT statements, removing duplicates. UNION ALL concatenates the results, and is more efficient, as it stops SQL removing duplicates between the two lists - in this case, we know they won't overlap due to the selection criteria being mutually exclusive.
You seem to be missing the point of a relational database, which is that it has no order other than the one you impose:
sSQL = "SELECT * FROM MyTable ORDER BY MyNumField DESC"
Set QDF = CurrentDB.CreateQueryDef ("DatOut", sSQL)
DoCmd.TransferText acExportDelim,,"DatOut","C:\Docs\Datout.csv"