I apologies in advance for this question – but I have no experience in classic ASP, although I have tried, I just cannot figure it out.
I have been given the task of tweaking an existing system (until a replacement is found), which has been written in ASP/VBScript.
The issue I am facing is that when I retrieve some data using SQL that I can’t get the script to update the results into the table. The number of results can change each time a query is run.
For example; The data in table calculates the time someone is absent, and each time someone is absent it records the duration in one field, in another field it records the date and in another field gives the record a value = 1.
This part works.
What I’m trying to do is collate the total duration absence per date and insert that data into the same table with a value = 2.
The code:
Set rssum = conn.execute("select sum(total) as total, visitdate from table_1 where repid=" & session("repid") & " and type=1 group by visitdate ")
If rssum.eof Then
While Not rssum.eof
rstotal=rssum("total")
rstotal=rssum("visitdate")
'some code to generate nextid
Set abrec = conn.execute(insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ")
Wend
rssum.movenext
rssum.close
Set rssum = Nothing
End If
When I run the script, and say I’m calculating 2 days worth of data, it returns 2 type=2 entries, but duplicates the date for the first date.
Why does this happen?
movenext needs to be at the end of the loop, but inside the loop. Otherwise it will repeat indefinitely. I'm surprised you didn't get an infinite loop.
while not rssum.eof
rstotal=rssum("total")
rstotal=rssum("visitdate")
'some code to generate nextid
set abrec = conn.execute(insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ")
rssum.movenext
wend
Several issues here.
1.
if rssum.eof then
while not rssum.eof
Think about the logic of this, you're saying if a condition exists then do this only when the condition doesn't exist. You probably don't need a conditional statement at all, but if you want one which only applies to a non empty recordset then the way to do it is
if not (rssum.eof and rssum.bof) then
2.
set abrec = conn.execute(...)
This is an insert command not a select. You don't have any output to populate a recordset, so lose set abrec =
You need quotes around your insert query
As Ekkehard points out you need to assign the values of rssum("total") and rssum("visitdate") to separate variables, don't use rstotal twice
As Kyle JV points out in another answer wend needs to come after rssum.movenext, not before.
Putting it all together, try
set rssum = conn.execute("select sum(total) as total, visitdate from table_1 where repid=" & session("repid") & " and type=1 group by visitdate ")
if not (rssum.bof and rssum.eof) then
while not rssum.eof
total=rssum("total")
visitdate=rssum("visitdate")
conn.execute("insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ")"
rssum.movenext
wend
rssum.close
set rssum = nothing
end if
if rssum.eof then ' if EOF
while not rssum.eof ' while not EOF
can't possibly work and assigning to rstotal twice:
rstotal=rssum("total")
rstotal=rssum("visitdate")
makes no sense either.
Related
I'm using this update query in MS access 2016
Update sampledata set VALUE= '" & Me.value & "' where Day between '" & Me.dayfrom & "' and '" & Me.dayto & "';
The strangest problem m facing is- It is considering the form values for start and end date but however updates records only for the start date. Example. If dayfrom is 01-Nov-2021 and dayto is 30-Nov-2021, the query is updating records of only 01-Nov-2021.
When I pass the day from as 30-Nov-2021, it is updating records for the whole month.
Note: This doesn't happen when I directly pass the values in the query, it happens only when i Pick data from FORM and apply it in query.
UPDATE table3
SET table3.status = "YES"
WHERE (((table3.transactiondate)>=[FORM]![Form3]![startdate] And
(table3.transactiondate)<=[FORM]![Form3]![enddate]));
When you run the query, two pop up input box will open, in the first one, enter the value for start date, e.g 01/01/2022 , in the second one enter the end date e.g 02/28/2022. Do check the date format in use by your system so you can be sure you are entering the date parameter in the right format.
As is, the date values will be casted to strings using your Windows settings.
So, force a format on the string expressions to have true string expressions for the date values:
Sql = "Update sampledata " & _
"Set VALUE = '" & Me!value.Value & "' " & _
"Where Day Between #" & Format(Me!dayfrom.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!dayto.Value, "yyyy\/mm\/dd") & "#;"
I am planning to search and update records which matches my criteria in my table through a form.
I want my code to search for OrderNo and OrderNoItem ( For each orderno I have multiple OrderNoItems like 10,20,30... in my table) when there is a match I want to update the customer name(Text18.Value) from my Form.
I have the following code. For some reason it is just updating only the first record. For example when I input Text25.Value = 12345, Text27.Value = 20 and Text49.Value = 40, it is updating the customer name only for the rows with 12345 and 20. Can any one help??
Set logDB1 = CurrentDb()
Set logRS1 = logDB1.OpenRecordset("Log")
For i = Text27.Value To Text49.Value Step 10
Do Until logRS1.EOF
If (logRS1![OrderNo] = Text25.Value And logRS1![OrderNoItem] = Text27.Value) Then
logRS1.Edit
logRS1![DTN-#] = Text18.Value
logRS1.Update
End If
logRS1.MoveNext
Loop
Next
Because code is set to do that. That's exactly what the If condition requires. What you should do is open the recordset filtered by OrderNo and the OrderNoItem range then just loop those records.
Set logDB1 = CurrentDb()
Set logRS1 = logDB1.OpenRecordset("SELECT [DTN-#] FROM Log WHERE OrderNo='" & _
Me.Text25 & "' AND Val(OrderNoItem) BETWEEN " & Me.Text27 & " AND " & Me.Text49)
Do Until logRS1.EOF
logRS1.Edit
logRS1![DTN-#] = Me.Text18
logRS1.Update
logRS1.MoveNext
Loop
Or instead of opening and looping recordset, Execute an UPDATE action SQL:
CurrentDb.Execute "UPDATE Log SET [DTN-#]='" & Me.Text18 & _
"' WHERE OrderNo = '" & Me.Text25 & "' AND Val(OrderNoItem) BETWEEN " & Me.Text27 & " AND " & Me.Text49
If OrderNo is a number field, remove the apostrophe delimiters.
Strongly advise not to use punctuation/special characters (underscore is only exception) in naming convention. Better would be DTN_Num.
I want to sort multiple fields in an access data base but being a newbie I took reference from MSDN program and first trying to sort on a single field. I'm getting error From syntax wrong on the following lines.
Set rst = dbs.OpenRecordset("SELECT ACT_CD, " _
& "SNO FROM Banks_Trnx_2018-2019" _
& "ORDER BY SNO DESC;")
The full program as follows.
Option Compare Database
Sub OrderByX()
Dim dbs As Database, rst As Recordset
' Modify this line to include the path to Northwind
' on your computer.
Set dbs = OpenDatabase("E:\FY_2018-2019\Banks\Banks_Trnx_2018-2019.accdb")
' Select the last name and first name values from
' the Employees table, and sort them in descending
' order.
Set rst = dbs.OpenRecordset("SELECT ACT_CD, " _
& "SNO FROM Banks_Trnx_2018-2019" _
& "ORDER BY SNO DESC;")
' Populate the Recordset.
rst.MoveLast
' Call EnumFields to print recordset contents.
' EnumFields rst, 12
dbs.Close
End Sub
I am clueless. Please help. As already mentioned I would like to sort on three fields (multiple fields) comprising of text and numerical fields which presently I can easily do by setting columns in proper order and selecting them together while sorting. Since it is a repetitive operation I am trying to develop VBA procedure for the same. Any pointer in the right direction shall be highly appreciated.
You should set your sql to a variable first - makes it a lot easier to troubleshoot - you'd be able to see you've left out some spaces between keywords and table
Set rst = dbs.OpenRecordset("SELECT ACT_CD, " _
& "SNO FROM [Banks_Trnx_2018-2019]" _
& "ORDER BY SNO DESC;")
Should be (with added brackets for table as suggested by #skkakkar)
Set rst = dbs.OpenRecordset("SELECT ACT_CD, " _
& "SNO FROM [Banks_Trnx_2018-2019] " _
& "ORDER BY SNO DESC;")
You have a - in your table name. I didn't even think that was allowed, either put brackets around your table name or change the table name.
"SELECT ACT_CD, " _
& "SNO FROM [Banks_Trnx_2018-2019]" _
& "ORDER BY SNO DESC;"
or
"SELECT ACT_CD, " _
& "SNO FROM [Banks_Trnx_2018_2019] " _
& "ORDER BY SNO DESC;"
and change table name.
I am using vb6, and the database is mysql. There is this table called "absen", it has a field called "tglabsen" which storing dates in this format : dd/mm/yyyy. I tried to find records according to the date.
eg. find records who have dates between 01/01/2017 to 01/02/2017
My question is how to store the number in a variable, and then display it in a textbox? What I tried so far, I tested this :
number = "Select count(*) from absen where tglabsen >='" & DTPicker1 & "' and tglabsen <='" & DTPicker2 & "'"
KON.Execute number
txtjumlahabsen = number
But the textbox (txtjumlahabsen) is just showing the sql query above.
KON.Execute just executes the sql statement you stored in your number variable. It doesn't update the variable with the data, which is why your textbox is showing the sql statement.
You need to open a recordset to retrieve the data:
Dim rs as New Recordset
Dim countVal as Integer
number = "Select count(*) from absen where tglabsen >='" & DTPicker1 & "' and tglabsen <='" & DTPicker2 & "'"
rs.Open number, KON, adOpenForwardOnly, adLockReadOnly
If Not rs.EOF then
countVal = rs(0).Value
End If
rs.Close
txtjumlahabsen.Text = countVal
If your sql statement is successful, the value from count(*) will be applied to the countVal variable. If it is not successful, countVal will remain at zero.
I have encountering a very silly problem. The requirement is to stop the duplicate entry in access table. I am using Vb6. But each time I try I am encountering syntax error.
My code
My flexgrid is populated and refreshed. I am able to insert and select data in another table in same database. But this one is failing
sql_txt1 = "Select SL_No from SpAIReport where DOM ='" & Format(Now, "mm/dd/yyyy") & _
"' and AC-REG ='" & msgDisFlex.TextMatrix(i, 4) & "' and Flt No = '" & msgDisFlex.TextMatrix(i, 6) & "'"
Set rs1 = db.OpenRecordset(sql_txt1)
I am able to update this table, but multiple time same data are getting populated.
The access table structure with the entries are given below
Date_OF_Fly Flt No GMT Weight Airtime Station DOM Data_hrs Filename
7/3/2000 11 03:45:01 5 01:23:40 XXX 01/25/2014 120:10:15 ABCD
Plus, after saving if I want to access it through recordset then it is showing NULL.
The code is:
sql_txt = "select * from SpAIReport where DOM='" & dateDailyReport & "'"
Set rs = db.OpenRecordset(sql_txt)
The dateDailyReport value is 01/25/2014. This value is present in database. Still this query is not working.
Please help.
You probably want to put 'Flt No' and 'AC-REG' in square brackets otherwise they look like two field names:
"' and [AC-REG] ='" & msgDisFlex.TextMatrix(i, 4) & "' and [Flt No] = '" &