Results 1 to 2 of 2
  1. #1
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56

    Drop/Create table in loop

    Hello again. I am trying to improve my program so that temp tables get dropped and recreated dynamically based on a variable name (which is the same as the table name I want to drop and recreate). Currently I have the drop and create statments hard coded at the top of the program like this



    Code:
    Public Sub Import()
    
    'DoCmd.SetWarnings (WarningsOff)
    'Establish a Connection to the Current Database
    Set db = CurrentDb()
    On Error Resume Next
    'Drop the tables
    DoCmd.RunSQL ("DROP TABLE GPS_NOT_CLIENT")
    DoCmd.RunSQL ("DROP TABLE CLIENT_NOT_GPS")
    DoCmd.RunSQL ("DROP TABLE ALL_DISCREPANCIES")
    'Create the tables
    DoCmd.RunSQL ("CREATE TABLE [GPS_NOT_CLIENT]")
    DoCmd.RunSQL ("CREATE TABLE [CLIENT_NOT_GPS]")
    DoCmd.RunSQL ("CREATE TABLE [ALL_DISCREPANCIES]")
    
    ...
    I want to improve the proram to do the following but it is not working

    Code:
    Public Sub Import()
    
    'DoCmd.SetWarnings (WarningsOff)
    'Establish a Connection to the Current Database
    Set db = CurrentDb()
    
    On Error Resume Next
    'Open File Dialog to choose file to process
    Set fd2 = Application.FileDialog(msoFileDialogFilePicker)
    With fd2
    '.AllowMultiSelect = True
      .Show
      For Each vrtSelected In .SelectedItems
       
         'MsgBox (vrtSelected)
         Set xlapp = CreateObject("Excel.application")
         Set xlWrkBk = GetObject(vrtSelected)
         If Not xlWrkBk Is Nothing Then
         '   MsgBox (xlWrkBk.Name)
         End If
        
         ws_count = xlWrkBk.Sheets.count
        ' MsgBox (ws_count)
    'Application.CutCopyMode = False
       t = 1
      'Loop First Row in Each Excel Worksheet which contains header column names
         For k = 1 To ws_count
            xlWrkBk.Activate
          '  xlapp.Visible = True
            
            Dim tSheetName As String
            
            tSheetName = xlWrkBk.Sheets(t).Name
    
            DoCmd.RunSQL ("DROP TABLE [ " & tSheetName & " ]")
            DoCmd.RunSQL ("CREATE TABLE [ " & tSheetName & " ]")   
    
           ....
    When I trying running the program with the DROP/CREATE inside the loop, nothing happens

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    From the snippet of code you posted, I see 2 things immediately:

    1) You have declared only one variable ("tSheetName ") and
    2)that variable is within two For..Next loops. That means that the variable "tSheetName" will be dimensioned every loop (or try to be dimensioned)

    All variable declarations should be (IMHO) at the top of the subroutine.

    Do you have
    Code:
    Option Compare Database
    Option Explicit
    as the first two lines of every module?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Create drop down box in form
    By marcvanderpeet12 in forum Forms
    Replies: 2
    Last Post: 08-02-2013, 08:51 PM
  2. Replies: 3
    Last Post: 03-10-2013, 07:04 AM
  3. create table using something like loop query
    By learning_graccess in forum Queries
    Replies: 20
    Last Post: 04-18-2012, 09:52 AM
  4. Replies: 3
    Last Post: 10-19-2011, 01:05 PM
  5. I can't create a drop-down list box
    By cpuser in forum Access
    Replies: 4
    Last Post: 02-18-2008, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums