you shouldnt, queries are better.
but,
in the query, click the design,SQL button
copy sql
put in vb code
sql = "select * from table"
sql = "SELECT Format(Int((108120-[Value])/3600),"00") AS Total FROM tbl1 WHERE (((tbl1.Property)="100"));"
sql = "SELECT Format(Int((108120-[Value])/3600),'00') AS Total FROM tbl1 WHERE (((tbl1.Property)='100'));"
You can't use double quotes (") in SQL statements.
You either need to use single quotes (') or double double quotes ("")
So for example, your query should probably be
Code:sql = "SELECT Format(Int((108120-[Value])/3600),'00') AS Total FROM tbl1 WHERE (((tbl1.Property)='100'));"
I say probably as I haven't tested the section Format .... AS.
I would also recommend my SQL to VBA & back again utility as a way of learning how to do such conversions. Its easy & it works!
To convert query SQL to VBA, do the following:
a) Change your query from design view to SQL view. Copy it to the clipboard
b) Open the utility. Click the SQL tab and paste in the query SQL
c) Click the VBA tab and the converted VBA code will be shown.
You can then copy this into a VBA procedure
DoCmd.RunSQL " SELECT Format(Int((108120-[Value])/3600),""00"") AS Total " & _
" FROM tbl1 " & _
" WHERE (((tbl1.Property)=""100""));"
i did that and i got thisnow how can i show the result on the form in the unbound textbox ?Code:DoCmd.RunSQL " SELECT Format(Int((108120-[Value])/3600),""00"") AS Total " & _ " FROM tbl1 " & _ " WHERE (((tbl1.Property)=""100""));"
=DLookUp("Total","q1")
Private Sub Form_Load()
Me.Text0.Value = DLookup("Total", "q1")
End Sub
Private Sub Form_Load()
Me.Label0.Caption = DLookup("Total", "q1")
End Sub
you cannot use dbl-quotes inside dbl-quotes:
sql = "SELECT Format(Int((108120-[Value])/3600),'00') AS Total FROM tbl1 WHERE (((tbl1.Property)='100'));"
Have a look at this excellent video from Phillip should sort out most of your formatting problems there...
VBA SQL Strings - Tutorial for Beginner
sql = "SELECT Format(Int((108120-[Value])/3600),'00') AS Total FROM tbl1 WHERE (((tbl1.Property)='100'));"
=DLookup("City", "Customers", "CompanyName = ""MendipDataSystems""")
=DLookup("City", "Customers", "CompanyName = """ & [CompanyName] & """")
Public Function fINSERT_AgeData(ByVal lngRptID As Long, ByVal lngChkID As Long, ByVal lngGrpID As Long, ByVal lngSetsID As Long, ByVal strAgeRangeDesc As String, ByVal lngAgeRangeCount As Long, ByVal strAgeRangeDate As String)
Dim strSQL0 As String
Dim strInsert As String
Dim strValues As String
'Example
'How to Format Text Data
strAgeRangeDesc = Chr(34) & strAgeRangeDesc & Chr(34)
'How to Format Date Data 1
strAgeRangeDate = Chr(35) & strAgeRangeDate & Chr(35)
strInsert = "INSERT INTO tblReporting (ReportID, CheckListID, GrpID, SetsID, ItemsText, ItemsCount, ReportDate) "
strValues = "VALUES (" & lngRptID & ", " & lngChkID & ", " & lngGrpID & ", " & lngSetsID & ", " & strAgeRangeDesc & ", " & lngAgeRangeCount & ", " & strAgeRangeDate & ");"
strSQL0 = strInsert & strValues
CurrentDb.Execute strSQL0
strSQL = strSELECT & strWHERE & strORDERBY & ";"
As well as being easier to read/edit, it also has the advantage of solving the
'too many line breaks' issue
Dim strSQL0 As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQL3 As String
Dim strSQL4 As String
Dim strSQL5 As String
Dim strSQL6 As String
Dim strSQL7 As String
Dim strSQL8 As String
Dim strSQL9 As String
strSQL1 = ""
strSQL2 = ""
strSQL3 = ""
strSQL4 = ""
strSQL5 = ""
strSQL6 = ""
strSQL7 = ""
strSQL8 = ""
strSQL9 = ""
strSQL0 = strSQL1 & strSQL2 & strSQL3 & strSQL4 & strSQL5 & strSQL6 & strSQL7 & strSQL8 & strSQL9
fSiteBy_PO = strSQL0
strSCase = String(12, " ") & "Dim strBase As String, dteDate As Date" & vbCrLf & vbCrLf & _
String(12, " ") & "|get common values" & vbCrLf & _
String(12, " ") & "strBase = result('base')" & vbCrLf & _
String(12, " ") & "dteDate = result('date')" & vbCrLf & vbCrLf & _
String(12, " ") & "|get string in array" & vbCrLf & _
String(12, " ") & "lngStart = InStr(1, strJSON, '''rates''') + 9" & vbCrLf & _
String(12, " ") & "lngEnd = InStr(lngStart, strJSON, '}')" & vbCrLf & _
String(12, " ") & "strTemp = Mid(strJSON, lngStart, lngEnd - lngStart)" & vbCrLf & _
String(12, " ") & "|parse string in array" & vbCrLf & _
String(12, " ") & "arrFieldsValues = Split(strTemp, ',')" & vbCrLf & vbCrLf & _
String(12, " ") & "For Each varitem In arrFieldsValues" & vbCrLf & _
String(16, " ") & "varitem = Replace(varitem, '''', '')" & vbCrLf & _
String(16, " ") & "arrTemp = Split(varitem, ':')" & vbCrLf & vbCrLf & _
String(16, " ") & ".AddNew" & vbCrLf & _
String(16, " ") & "!Currency = arrTemp(0)" & vbCrLf & _
String(16, " ") & "!Rate = arrTemp(1)" & vbCrLf & _
String(16, " ") & "!Base = strBase" & vbCrLf & _
String(16, " ") & "!Date = dteDate" & vbCrLf & _
String(16, " ") & ".Update" & vbCrLf & _
String(12, " ") & "Next"
'code for table 1 - tblCurrencyExchange
Set rst = db.OpenRecordset("tblCurrencyExchange", dbOpenDynaset, dbSeeChanges)
With rst
For Each result In JSON("result")
'-------------------------------------
Dim strBase As String, dteDate As Date
'get common values
strBase = result("base")
dteDate = result("date")
'get string in array
lngStart = InStr(1, strJSON, """rates""") + 9
lngEnd = InStr(lngStart, strJSON, "}")
strTemp = Mid(strJSON, lngStart, lngEnd - lngStart)
'parse string in array
arrFieldsValues = Split(strTemp, ",")
For Each varitem In arrFieldsValues
varitem = Replace(varitem, """", "")
arrTemp = Split(varitem, ":")
.AddNew
!Currency = arrTemp(0)
!Rate = arrTemp(1)
!Base = strBase
!Date = dteDate
.Update
Next
'-------------------------------------
Next
.Close
End With