thank you for your time. I've put stars on the error line if this helps.
I'm lost with an error and I can't find the reason. Can I get some help, please? The macros is not running for only one period and I didn't find any different data in the database compare to the previous period
That's the debug menu:
Get values and convert to string for text box:
a = Format(.Cells(rowNum, dateCol), "dd mmm")
a = a + ", " + Str(.Cells(rowNum, actualAssetCol))
Get values and convert to string for text box:
a = Format(.Cells(rowNum, dateCol), "dd mmm")
a = a + ", " + Str(.Cells(rowNum, actualAssetCol))`
The string concatenation operator in vba is & and not +. Thus, use it like this:
a = a & ", " & Str(.Cells(rowNum, actualAssetCol))`
Related
I'm doing a simple DCount, just looking for how many people have signed up for the same date and time. At the moment I'm just using a single date and a single time to prove the process, then I'll have it loop through all possible dates and times.
Private Sub Get_Singles()
Dim TestDate As String
Dim TestTime As String
AloneCnt = 0
Dinc = 0
Tinc = 0
TestDate = vStartDate
TestTime = "0700"
If (DCount("[ID]", VigilTable, "[fldTime] = " & TestTime) = 1) Then
' "[fldDate] = " & TestDate) & " And
AloneCnt = AloneCnt + 1
End If
End Sub
It works fine for the date (I've moved it to a different line and commented it out so I can focus on the time.).
In the table, fldDate and fldTime are both set for text (shows up as Field Size
= 255 in the properties list) and, as you can see, TestDate and TestTime are both dimmed as String.
And it works if I change the DCount line to:
(DCount("[ID]", VigilTable, "[fldTime] = '0700'")
So where's the error?
Thanks.
The error is, that you must handle date and time as data type Date. So change the data type of the fields to DateTime and:
Dim TestDate As Date
Dim TestTime As Date
Dim AloneCnt As Long
TestDate = vStartDate
TestTime = TimeSerial(7, 0, 0)
If DCount("*", "VigilTable", "[fldTime] = #" & Format(TestTime, "hh\:nn\:ss") & "# And [fldDate] = #" & Format(TestDate, "yyyy\/mm\/dd") & "#") = 1 Then
AloneCnt = AloneCnt + 1
End If
Because you hold all date/time bits as text/string, you should use the text syntax in the criteria as you showed: with quotes (notice the quotes around time string):
(DCount("[ID]", VigilTable, "[fldTime] = '" & TestTime & "'")
You could also keep Date/times in date types like #Gustav's answer.
Update:
You must be doing something wrong; here is my suggested version in VBA window, and it is not Red.
OK, I found the fix after searching several other sites; it was, as I suggested above, a problem of handling variables:
If (DCount("[ID]", VigilTable, "[fldDate]='" & TestDate & "'" & " AND [fldTime] = '" & TestTime & "'") = 1) Then
I always have trouble with those damned single quotes and can never get them in the right place (I'd tried placing single quotes several times but never got them where they are here.); I don't know if the rules are different in different places or I just don't understand the rules or both.
Thanks for all your suggestions; even things that don't work help.
Im trying to make a app for cafe. It has a simple interface coffee names with labels and quantity with numericupdown, receipt textbox and receipt button. I coded receipt button to show coffee name and quantity in receipt textbox like this:
If (espresso.Value > 0) Then receipt.AppendText("Espresso" + vbTab + vbTab + espresso.Value.ToString + Environment.NewLine)
that works fine but i want to add the price next to quantity of the coffee so i added these lines :
Dim espressoprice As Double
espressoprice = 3
Dim espressoquantity As Double = Convert.ToDouble(espresso.Value)
Dim espressototal As Double
espressototal = (espressoprice * espressoquantity)
(espresso.value is numericupdown value)
and changed the first codeline like this:
If (espresso.Value > 0) Then receipt.AppendText("Espresso" + vbTab + vbTab + espresso.Value.ToString + vbTab + espressototal + Environment.NewLine)
but i keep getting this error:
"Espresso 2 " "Conversion from string "" to type 'Double' is not valid."
What am i doing wrong please help.
The proper solution to this problem is to use the correct operator. You are trying to perform string concatenation but you are using the addition operator. This:
"Espresso" + vbTab + vbTab + espresso.Value.ToString + vbTab + espressototal + Environment.NewLine
is actually performing multiple additions. Addition maps to concatenation for two Strings but for numbers, addition is mathematical, NOT textual. In order to add a String and a numeric value, the system has to implicitly convert one of them to the other type. You are obviously assuming that the number will be converted to a String but it's actually the opposite that is happening, i.e. the system is trying to convert a String to a number and it is failing. This is why you should not rely on implicit conversions. If you used the concatenation operator, as you should when performing concatenation, then there's only one way it can go:
"Espresso" & vbTab & vbTab & espresso.Value.ToString & vbTab & espressototal & Environment.NewLine
Notice that, in this case, you don't have to explicitly convert the number to a String because the concatenation operator is defined for Strings and numeric values. Concatenation is a String operation so you know for a fact that everything that can be treated as a String, will be.
That said, there are better options anyway, e.g.
receipt.AppendText(String.Concat("Espresso", vbTab, vbTab, espresso.Value, vbTab, espressototal, Environment.NewLine)
In your line where you added expressototal you need to convert its value to a string in order to add it to other strings, that is, expressototal.ToString.
I have a query that I execute through VBA in Access 2010. The result of the query should be AFR, but it returns AFR with an extra line below it. I have added the "'" character to make the extra line visible.
TempHold = rs.GetString
Debug.Print "'" & TempHold & "'"
Returns this:
'AFR
'
But should return this:
'AFR'
I have tried using the below code, but none of the If statements evaluate as True. The code should check for a " ", a vbNewLine, or vbCrLf character but none evaluate as true. Does anyone know of any additional characters that would result in a new line?
If Right(TempHold, 1) = " " Then
TempHold = Left(TempHold, Len(TempHold) - 1)
ElseIf Right(TempHold, 2) = vbNewLine Or Right(TempHold, 2) = vbCrLf Then
TempHold = Left(TempHold, Len(TempHold) - 2)
End If
Use:
Asc(Right(TempHold, 1))
to get the Ascii character code.
Once you've found the character code (which, as you wrote in your comment, was 13), you can use your code to remove it:
If Right(TempHold, 1) = Chr(13) Then
TempHold = Left(TempHold, Len(TempHold) - 1)
End If
In this case, you can also use vbCr, which is the same as Chr(13).
The best way to get rid of the carriage return in my opinion is to stop it being created in the first place. This method is a lot tidier than having to remove the last character.
In the .GetString method there is a parameter for RowDelimiter which by default is set to be a carriage return. However you can change this to be whatever you want including a zero length string as follows:
rs.GetString(, , , "")
If you run your debug again with this code:
rs.GetString(, , , "")
Debug.Print "'" & TempHold & "'"
You will get this result:
'AFR'
Remember if you want something different to be placed between rows then just change the zero length string to whatever you need.
I'm new in vb and crystal report so please help me from my problem. I'm assigning values using vb codes to a formula field from my crystal report. Here is my code:
Dim report As CrystalDecisions.CrystalReports.Engine.ReportDocument
report = New report_Student()
report.DataDefinition.FormulaFields("student").Text = "Slone" & Chr(13) & "Thompson"
frm_print.viewerReport.ReportSource = report
frm_print.viewerReport.RefreshReport()
frm_print.Show()
i place this in button click event. Now i have an error on running this, before it loads the crystal report from viewer this error shows:
The remaining text does not appear to be part of the formula.
Details: errorKind
Error in formula student:
'Slone
'
I guess as you have formula field (not text object) you should use formula there:
report.DataDefinition.FormulaFields("student").Text = _
ControlChars.Quote + "Slone" + ChrW(13) + "Thompson" + ControlChars.Quote
So, in result formula will contain one text literal ("Slone\nThompson").
Not tried this, but hope it should work.
If not, then probably you need to use CR inner function ChrW() for newline in next way:
report.DataDefinition.FormulaFields("student").Text = _
ControlChars.Quote + "Slone" + ControlChars.Quote + _
" + ChrW(13) + " + _
ControlChars.Quote + "Thompson" + ControlChars.Quote
So, in result two text literals ("Slone" and "Thompson") will be concatenated with result of CR inner function ChrW().
In CR formula editor it will be shown as
"Slone" + ChrW(13) + "Thompson"
But I expect 1st way should work.
You should use a report parameter instead of a formula. In case you need to modify the values inside the report use the parameter inside the formula
Using the following query in microsoft access sql view works nice and easy with a hard coded date
SELECT Salary.First, Salary.Last,FROM Salary, allowances
WHERE Salary.PayThroughDate = CDate("2014-05-06") AND Salary.SSN = allowances.SSN
but embedding this query in Vba using a variable instead of a hard coded date is another business. It is just not working:
Dim ddate As Variant
Dim getDay As Integer
Dim getMonth As Integer
Dim getYear As Integer
getDay = Day(Me.DTPicker2.Value)
getMonth = Month(Me.DTPicker2.Value)
getYear = Year(Me.DTPicker2.Value)
ddate = getDay & "/" & getMonth & "/" & getYear
ddate = Format(ddate, "dd/mm/yyyy")
query1 = "SELECT Salary.First, Salary.Last FROM Salary, allowances WHERE Salary.PayThroughDate = " & CDate(ddate) & " AND Salary.SSN =
allowances.SSN
Any ideas in this Vba Sql mix? Am I missing single or double quotes?
When you send the SQL directly to JET and not running it from the normal user-interface of MS Access, you will need to make the date-format in the American format. I have had this problem a lot long time ago, but solved it by using this function to format my date in to text, the way that JET expects it to be:
Function SQLDate(varDate As Date) As String
'---------------------------------------------------------------------------------------
'Purpose Formats a date in the american way so that it can be used in
' SQL (by the JET-engine)
'Accepts varDate - the date that should be converted to text
'Returns The date converted to text, in american format with both the day and
' the month using 2 characters. (01 is january)
'---------------------------------------------------------------------------------------
'Changelog
'HANY 20100407: Stopped using the FORMAT-function for the date-value, as I found some
' cases where the format did not return the date as specified.
'---------------------------------------------------------------------------------------
'SQLDate = "#" & Format$(varDate, "mm\/dd\/yyyy") & "#"
SQLDate = "#" & Format(Month(varDate), "00") & "/" & Format(Day(varDate), "00") & "/" & Format(Year(varDate), "0000") & "#"
End Function
In your example above, you now add the date like this & SQLDate(CDate(ddate)) & instead of & CDate(ddate) &
The equivilant query is:
query1 = "SELECT Salary.First, Salary.Last
FROM Salary, allowances
WHERE (Salary.PayThroughDate =Cdate(" & ddate & ") )
AND (Salary.SSN = allowances.SSN)"
The problem you had is that when you constructed the sql string, the cdate value was converted into a string value first. It was just the same as
WHERE (Salary.PayThroughDate =" & format(ddate,"general date") & ")
If you do the cdate conversion later it works. Another almost equivilant query is
...
WHERE Salary.PayThroughDate =#" & ddate & "#
...
Both Cdate and the # tags take a string date, and convert to a date type (using slightly different rules). Since a date type is actually stored as the integer part of a double, you can also do:
WHERE Salary.PayThroughDate =cdbl(#" & ddate & "#)
or
WHERE Salary.PayThroughDate =clng(#" & ddate & "#)
or
WHERE Salary.PayThroughDate =" & clng(cdate(ddate) & "
--the number will be converted into a string to be part of the sql string, but numbers work correctly in SQL: they don't need to have # tags or conversion functions to make them work.
but you started out with a date value:
v =Me.DTPicker2.Value
so you can convert that directly to a number without converting it to a string first:
d = clng(Me.DTPicker2.Value)
...
WHERE (Salary.PayThroughDate = " & d & ")
which is the fastest and least error prone of handling dates in Access/VBA, but comes undone when you start working with SQL server, which stores dates differently.
Also, I see that you correctly used ISO date format when constructing your test example, but switched to American date format when constructing a string from the date value. That was a mistake. To avoid errors and 'evil date guessing' in SQL, you should stick to the ISO format:
v =Me.DTPicker2.Value
sdate = format(v,"yyyy-mm-dd")