Results 1 to 16 of 16

Thread: Temporary Table

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    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

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    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:
    Code:
    DROP TABLE t_test
    I got all the above information from here
    This world is not my home. I'm just passing through.

  3. #3
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Temporary Table

    One could often use Temporary tables for very complex queries.

    Thing is, I don't believe temporary tables exist in Access.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Temporary Table

    Quote 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.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Temporary Table

    Rofl.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    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:
    1. Private Sub CmdPreview_Click()
    2.     CmdPreview.Enabled = False
    3.     Dim strRepName As String
    4.     Dim SqlString As String
    5.     Set cmdobj = New Command
    6.     Set ComObjUpd = New Command
    7.     Set RoGenLed = New ADODB.Recordset
    8.     Set RtGenLed = New ADODB.Recordset
    9.     Dim opamt As Double
    10.     Dim opac As String
    11.    
    12. '   Create Temporary Table - Preview Report
    13.     With cmdobj
    14.         .ActiveConnection = ConnectAcct
    15.         .CommandText = "SELECT * INTO OGENLED FROM GENLED WHERE DOCDATE <#" & TxtFromDate.Text & "# OR (OAMOUNT <> 0 AND DOCDATE IS NULL) ORDER BY GLNAME"
    16.         .Execute
    17.         .CommandText = "SELECT * INTO TGENLED FROM GENLED WHERE DOCDATE >=#" & TxtFromDate.Text & "# AND DOCDATE<=#" & TxtToDate.Text & "# ORDER BY GLNAME"
    18.         .Execute
    19.     End With
    20.  
    21. '   Update Opening Amount from Ogenled table to TGENLED table
    22.     With cmdobj
    23.         .ActiveConnection = ConnectAcct
    24.         .CommandText = "select glname, oamount, sum(tamount) as tamt from ogenled group by glname, oamount order by glname"
    25.     End With
    26.     Set RoGenLed = cmdobj.Execute
    27.     RtGenLed.Open "TGENLED", ConnectAcct, adOpenKeyset, adLockPessimistic, adCmdTable
    28.     If Not ((RoGenLed.BOF = True) And (RoGenLed.EOF = True)) Then
    29.         RoGenLed.MoveFirst
    30.         Do While Not RoGenLed.EOF
    31.             opamt = 0
    32.             opac = RoGenLed!glname
    33.             If IsNull(RoGenLed!tamt) Then
    34.                 opamt = RoGenLed!oamount
    35.             Else
    36.                 opamt = RoGenLed!oamount + RoGenLed!tamt
    37.             End If
    38.             If opamt <> 0 Then
    39.                 If Not RtGenLed.EOF And Not RtGenLed.BOF Then
    40.                     RtGenLed.MoveFirst
    41.                 End If
    42.                 Do While Not RtGenLed.EOF
    43.                     If UCase(opac) = UCase(RtGenLed!glname) Then
    44.                         RtGenLed!oamount = opamt
    45.                         RtGenLed.Update
    46.                         GenFound = True
    47.                     End If
    48.                     RtGenLed.MoveNext
    49.                 Loop
    50.                 'Not found then add new
    51.                 If GenFound = False Then
    52.                     RtGenLed.AddNew
    53.                     RtGenLed!glname = opac
    54.                     RtGenLed!oamount = opamt
    55.                     RtGenLed.Update
    56.                 End If
    57.             End If
    58.             RoGenLed.MoveNext
    59.         Loop
    60.     End If
    61.     RtGenLed.Requery
    62.     RoGenLed.Close
    63.     RtGenLed.Close
    64.  
    65. '   Report Preview in Crystal Report
    66.     strRepName = "Ledger.rpt"
    67.     SqlString = "SELECT * FROM TGENLED ORDER BY GLNAME, DOCDATE"
    68.     Me.CrystalReport1.ReportFileName = strRepName
    69.     Me.CrystalReport1.SQLQuery = SqlString
    70.     Me.CrystalReport1.Destination = crptToWindow
    71.     Me.CrystalReport1.Connect = "dsn= " & DsnName & ""
    72.     Me.CrystalReport1.Formulas(0) = "compname = '" & CompanyName & "'"
    73.     Me.CrystalReport1.Formulas(1) = "rfdate = '" & TxtFromDate.Text & "'"
    74.     Me.CrystalReport1.Formulas(2) = "rtdate = '" & TxtToDate.Text & "'"
    75.     Me.CrystalReport1.Action = 1
    76.  
    77. '   Drop Temporary Table - Preview Report
    78.     With cmdobj
    79.         .ActiveConnection = ConnectAcct
    80.         .CommandText = "DROP TABLE OGENLED"
    81.         .Execute
    82.         .CommandText = "DROP TABLE TGENLED"
    83.         .Execute
    84.     End With
    85.     CmdPreview.Enabled = True
    86.     cmdobj.ActiveConnection = Nothing
    87.     Set RoGenLed = Nothing
    88.     Set RtGenLed = Nothing
    89. End Sub
    Can it possible with query or single table.
    pls guide because this will take too much time.


    thanks

    asm

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
      1. SqlString = "select glname, oamount, sum(tamount) as tamt from ogenled group by glname, oamount order by glname"
      2.     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:
      1. 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:
    1. sqlString = "SELECT * " _
    2.           & "FROM GENLED " _
    3.           & "WHERE (OAMOUNT <> 0 AND DOCDATE IS NULL) " _
    4.           & "OR DOCDATE <#" & TxtFromDate.Text & "# " _
    5.           & "OR (DOCDATE >=#" & TxtFromDate.Text & "# AND DOCDATE<=#" & "TxtToDate.Text & "#) " _
    6.           & "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.

  9. #9
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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 <"

  11. #11
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  13. #13
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Temporary Table

    HAVING operates on the query results.
    BETWEEN works like >= ...<=, but not sure at what point.
    Tengo mas preguntas que contestas

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  16. #16
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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
  •  



Click Here to Expand Forum to Full Width