|
-
Nov 8th, 2005, 05:37 AM
#1
Thread Starter
Hyperactive Member
Temporary Table
Hi
I have to create a temporary table for generating a report in VB.
Pls help how to check the temporary table name in database.
I want if exits than drop and create a new one.
(database : access2000)
thanks
asm
-
Nov 8th, 2005, 05:54 AM
#2
Re: Temporary Table
I believe that this code will give you a list of tables in an Access database. I haven't been able to test it though.
Code:
SELECT Name FROM MSysObjects
WHERE Type=1 AND Flags=0
Dropping tables is simple:
I got all the above information from here
This world is not my home. I'm just passing through.
-
Nov 8th, 2005, 06:11 AM
#3
Re: Temporary Table
What rally springs to mind is this:
Why do you need a table instead of a Query?
But you want an anwer instead of a question I suppose.....
Code:
Sub AddTable()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
' you should look at the optional arguments too
Set db = DAO.OpenDatabase("Yourdatabase")
Set tbl = db.CreateTableDef("Test2")
tbl.Fields.Append tbl.CreateField("Id", dbLong)
tbl.Fields.Append tbl.CreateField("Name", dbText, 50)
tbl.Fields.Append tbl.CreateField("Stmp", dbDate)
db.TableDefs.Append tbl
Set tbl = Nothing
db.Close
Set db = Nothing
End Sub
will make a table the SQL DROP table as suggested will delete it
you could also use the SQL CREATE TABLE to build one but in Access DAO should work fine
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Nov 8th, 2005, 07:28 AM
#4
Re: Temporary Table
One could often use Temporary tables for very complex queries.
Thing is, I don't believe temporary tables exist in Access.
-
Nov 8th, 2005, 07:58 AM
#5
Re: Temporary Table
 Originally Posted by mendhak
Thing is, I don't believe temporary tables exist in Access.
Sure they do....you just create a table you want to use temporarily and then DROP it afterwards.
-
Nov 8th, 2005, 08:03 AM
#6
Re: Temporary Table
Rofl.
-
Nov 9th, 2005, 05:51 AM
#7
Thread Starter
Hyperactive Member
Re: Temporary Table
Hi
I am generating a accounts ledger (using cr6)
1. generate query (GenLed)
2. from vb6 - create two tables Ogenled & Tgenled from query genled
Ogenled - Data before from date enter by user
Tgenled - Data between from date - To date enter by user
3. Update tgenled table from ogenled table (summary of amount) as opening in tgenled (if code found in tgenled else addnew in tgenled)
VB Code:
Private Sub CmdPreview_Click()
CmdPreview.Enabled = False
Dim strRepName As String
Dim SqlString As String
Set cmdobj = New Command
Set ComObjUpd = New Command
Set RoGenLed = New ADODB.Recordset
Set RtGenLed = New ADODB.Recordset
Dim opamt As Double
Dim opac As String
' Create Temporary Table - Preview Report
With cmdobj
.ActiveConnection = ConnectAcct
.CommandText = "SELECT * INTO OGENLED FROM GENLED WHERE DOCDATE <#" & TxtFromDate.Text & "# OR (OAMOUNT <> 0 AND DOCDATE IS NULL) ORDER BY GLNAME"
.Execute
.CommandText = "SELECT * INTO TGENLED FROM GENLED WHERE DOCDATE >=#" & TxtFromDate.Text & "# AND DOCDATE<=#" & TxtToDate.Text & "# ORDER BY GLNAME"
.Execute
End With
' Update Opening Amount from Ogenled table to TGENLED table
With cmdobj
.ActiveConnection = ConnectAcct
.CommandText = "select glname, oamount, sum(tamount) as tamt from ogenled group by glname, oamount order by glname"
End With
Set RoGenLed = cmdobj.Execute
RtGenLed.Open "TGENLED", ConnectAcct, adOpenKeyset, adLockPessimistic, adCmdTable
If Not ((RoGenLed.BOF = True) And (RoGenLed.EOF = True)) Then
RoGenLed.MoveFirst
Do While Not RoGenLed.EOF
opamt = 0
opac = RoGenLed!glname
If IsNull(RoGenLed!tamt) Then
opamt = RoGenLed!oamount
Else
opamt = RoGenLed!oamount + RoGenLed!tamt
End If
If opamt <> 0 Then
If Not RtGenLed.EOF And Not RtGenLed.BOF Then
RtGenLed.MoveFirst
End If
Do While Not RtGenLed.EOF
If UCase(opac) = UCase(RtGenLed!glname) Then
RtGenLed!oamount = opamt
RtGenLed.Update
GenFound = True
End If
RtGenLed.MoveNext
Loop
'Not found then add new
If GenFound = False Then
RtGenLed.AddNew
RtGenLed!glname = opac
RtGenLed!oamount = opamt
RtGenLed.Update
End If
End If
RoGenLed.MoveNext
Loop
End If
RtGenLed.Requery
RoGenLed.Close
RtGenLed.Close
' Report Preview in Crystal Report
strRepName = "Ledger.rpt"
SqlString = "SELECT * FROM TGENLED ORDER BY GLNAME, DOCDATE"
Me.CrystalReport1.ReportFileName = strRepName
Me.CrystalReport1.SQLQuery = SqlString
Me.CrystalReport1.Destination = crptToWindow
Me.CrystalReport1.Connect = "dsn= " & DsnName & ""
Me.CrystalReport1.Formulas(0) = "compname = '" & CompanyName & "'"
Me.CrystalReport1.Formulas(1) = "rfdate = '" & TxtFromDate.Text & "'"
Me.CrystalReport1.Formulas(2) = "rtdate = '" & TxtToDate.Text & "'"
Me.CrystalReport1.Action = 1
' Drop Temporary Table - Preview Report
With cmdobj
.ActiveConnection = ConnectAcct
.CommandText = "DROP TABLE OGENLED"
.Execute
.CommandText = "DROP TABLE TGENLED"
.Execute
End With
CmdPreview.Enabled = True
cmdobj.ActiveConnection = Nothing
Set RoGenLed = Nothing
Set RtGenLed = Nothing
End Sub
Can it possible with query or single table.
pls guide because this will take too much time.
thanks
asm
-
Nov 9th, 2005, 02:11 PM
#8
Re: Temporary Table
There are a few code "issues" in the section "' Update Opening Amount from Ogenled table to TGENLED table", such as:
- You do not need to use a Command to open a recordset with SQL, you could do this instead:
VB Code:
SqlString = "select glname, oamount, sum(tamount) as tamt from ogenled group by glname, oamount order by glname"
RoGenLed.Open SqlString, ConnectAcct, adOpenKeyset, adLockPessimistic, adCmdText
- There is no need for "RoGenLed.MoveFirst", as this is done automatically.
- The line "If Not ((RoGenLed.BOF = True) And (RoGenLed.EOF = True)) Then" has redundant parts, this would do the same job a little quicker:
VB Code:
If Not (RoGenLed.BOF And RoGenLed.EOF) Then
- There is no need for a Requery at the end - all this does is reload the data
Of course the quickest method is not to use VB code at all (as SQL is quicker), and the entire section can be replaced by these two SQL statements:
Code:
UPDATE tgenled
SET oamount = o.oamount + NZ(o.tamt,0)
FROM ogenled o
WHERE tgenled.glname = o.glname
Code:
INSERT INTO tgenled (glname, oamount)
SELECT o.glname, o.oamount + NZ(o.tamt,0)
WHERE UCase(o.glname) NOT IN (SELECT UCase(glname) FROM tgenled)
Or, even better, look at the SQL that you use to generate the two temporary tables - they can be combined into one fairly easily. As they can be combined, you could just use this as the SQL for the report rather than creating the table(s) at all!
I think this would work:
VB Code:
sqlString = "SELECT * " _
& "FROM GENLED " _
& "WHERE (OAMOUNT <> 0 AND DOCDATE IS NULL) " _
& "OR DOCDATE <#" & TxtFromDate.Text & "# " _
& "OR (DOCDATE >=#" & TxtFromDate.Text & "# AND DOCDATE<=#" & "TxtToDate.Text & "#) " _
& "ORDER BY GLNAME
I'm not 100% sure, but I think that Between would do the same as the last 'or' line, and would probably be a little quicker.
-
Nov 9th, 2005, 02:30 PM
#9
Re: Temporary Table
Between is slower in most cases.
Between is executed when the data has been tranfered to the recordset (it's a filter like instruction) and WHERE is executed before the data is transfered in normal recordsets (hirarchal recordsets are a diffrent ballgame)
So stick to WHERE
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Nov 9th, 2005, 02:33 PM
#10
Re: Temporary Table
What??? That doesn't make any sense to me, Between is as much a part of the Where clause as the > and < signs it is used to replace.
My only doubt is whether it is the equivalent of ">= AND <=" or "> AND <"
-
Nov 9th, 2005, 05:04 PM
#11
Re: Temporary Table
I've got it from a pretty reliable source ->
Frank balena, programming visual basic 6.0 (Microsoft press)
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Nov 9th, 2005, 06:25 PM
#12
Re: Temporary Table
I would be very surprised if it is any different from "> and <", as that is essentially what the SQL means. I know that on SQL Server there is no difference between the two, and I would be surprised if Access is any different.
I'm guessing that you may have mis-interpreted what was said, or it applies to something different. I would be interested to read it tho, if you can post the section that explains that.
-
Nov 10th, 2005, 12:05 PM
#13
Re: Temporary Table
I could be mistaken between and having but I'll look it up for you.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Nov 10th, 2005, 12:32 PM
#14
Frenzied Member
Re: Temporary Table
HAVING operates on the query results.
BETWEEN works like >= ...<=, but not sure at what point.
Tengo mas preguntas que contestas
-
Nov 10th, 2005, 02:03 PM
#15
Re: Temporary Table
Yep, Having works on the 'results', and should only be used for computed columns created as part of the Group By.
For most DBMS's Between works in the same manner as using >= etc, ie: within the initial Where clause, in order to build the result set.
-
Nov 10th, 2005, 03:10 PM
#16
Re: Temporary Table
searched long and hard but I have been mixing them up.
>, <, = , Like and between are all proccesed the same.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|