Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > Access SQL - Stumped!


Reply
 
Thread Tools Display Modes
  #1  
Old 01-17-2002, 04:36 PM
diver diver is offline
Senior Contributor

* Expert *
 
Join Date: Jun 2001
Location: Illinois
Posts: 865
Unhappy Access SQL - Stumped!


Does anyone know how to do this?

I have one table called "Sales" that is full of data.
I've created a blank table in Microsoft Access, which is a replica of the "Sales" table, let's call it "SalesTemp"

Table1: SALES (full of data)
Table2: SALESTEMP (same structure, no data)

I would like to be able to insert an ENTIRE COLUMN of data from Table1 into Table2 straight from SQL Syntax. No ADO, no looping, just some kind of simple SQL command.

Anyone know how to do this?

Thanks.
Diver
Reply With Quote
  #2  
Old 01-17-2002, 04:46 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Big maybe.
Code:
Insert Into SalesTemp (field1) (select field1 from Sales)
__________________
Posting Guidelines
Reply With Quote
  #3  
Old 01-17-2002, 05:07 PM
diver diver is offline
Senior Contributor

* Expert *
 
Join Date: Jun 2001
Location: Illinois
Posts: 865
Smile

Thinker,

That was too easy for you. I'll go and try it soon.
Your supposed to pretend that you had to really "think" to come up with the answer. Oh well... just losing your touch, huh?

Okay... heres one for you, I've been wondering about this as well... what the heck...

Let's say Table1 has a field filled with data (Nulls are allowed), and I want to use an SQL statement in Access to RENAME a column.

Table1/Column Name is: MONTH1

I want to rename it to: MONTH2


Still humble,
Diver
Reply With Quote
  #4  
Old 01-17-2002, 05:15 PM
diver diver is offline
Senior Contributor

* Expert *
 
Join Date: Jun 2001
Location: Illinois
Posts: 865
Default

Bummer!

Well, you know what they say... if it looks too good to be true, it might be!

Insert doesn't work... perhaps we are close?

Diver
Reply With Quote
  #5  
Old 01-17-2002, 05:29 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

I have looked for any way to use Alter Table Alter Column to
rename a column, and just can't find a way. Doesn't seem fair.
I can change the column from number to text, but not rename.
__________________
Posting Guidelines
Reply With Quote
  #6  
Old 01-17-2002, 05:35 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

I just tried that insert into. Don't ask me why, but as soon as I
took the select statement out of the parenthesis, it worked!
__________________
Posting Guidelines
Reply With Quote
  #7  
Old 01-17-2002, 05:36 PM
pisoftwar pisoftwar is offline
Centurion
 
Join Date: Nov 2001
Location: Trinidad
Posts: 142
Default

INSERT INTO test IN 'db2.mdb'
SELECT test.*
FROM test

You can Create this in the Query Designer

ALTER TABLE table {ADD {COLUMN field type[(size)] [NOT NULL] [CONSTRAINT index] |
ALTER COLUMN field type[(size)] |
CONSTRAINT multifieldindex} |
DROP {COLUMN field I CONSTRAINT indexname} }

Straight from the Help File look for Alter.
Access Supports some/most SQL Standards.
__________________
K.I.S. Keep It Simple.
Reply With Quote
  #8  
Old 01-17-2002, 05:38 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

But there is no way to rename a column using alter table alter
column. That was diver's question.
__________________
Posting Guidelines
Reply With Quote
  #9  
Old 01-17-2002, 05:41 PM
pisoftwar pisoftwar is offline
Centurion
 
Join Date: Nov 2001
Location: Trinidad
Posts: 142
Default

The only way that I have come close is by using ADOX.
No SQL to rename

In SQL you do this
Create the new column
Copy the Data
Drop the old column

Lame Huh
__________________
K.I.S. Keep It Simple.
Reply With Quote
  #10  
Old 01-17-2002, 06:01 PM
diver diver is offline
Senior Contributor

* Expert *
 
Join Date: Jun 2001
Location: Illinois
Posts: 865
Default

Code:
Thinker wrote:
I just tried that insert into. Don't ask me why, but as soon as I 
took the select statement out of the parenthesis, it worked!
Thinker,
You planned that all along didn't you!
Now, if I knew you to be a joker I might really think this is new kind of twisted vb humor!


Thanks bud.
Diver
Reply With Quote
  #11  
Old 01-17-2002, 06:03 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Thumbs up

I told you it was a big maybe. I learn as much from these questions
as you do, that is why I like them.
__________________
Posting Guidelines
Reply With Quote
  #12  
Old 01-17-2002, 07:02 PM
diver diver is offline
Senior Contributor

* Expert *
 
Join Date: Jun 2001
Location: Illinois
Posts: 865
Default

Okay guys...

So...

No renaming of columns allowed.
Renaming of TABLES allowed?

Diver
Reply With Quote
  #13  
Old 01-17-2002, 09:19 PM
Robby's Avatar
Robby Robby is offline
Code Factory

Retired Moderator
* Expert *
 
Join Date: Jan 2001
Location: Montreal, Ca.
Posts: 5,565
Default

To rename or create new fields....

Code:
Private Function sbPrepareFields(strTableName As String, _
        strFieldToRename As String, strFieldNewName As String, _
        strFieldFromScratch As String) As Boolean

    Dim dbEng As DBEngine
    Dim db As Database
    Dim wrk As Workspace
    Set dbEng = New DBEngine
    Set wrk = dbEng.Workspaces(0)
    Set db = wrk.OpenDatabase(App.Path & "\Sample.mdb")  'This line in VB
    'Set db = CurrentDb  'Use this Line if within Access
    
    Dim tdfNew As TableDef
    Set tdfNew = db.CreateTableDef(strTableName)
    Dim x As Object
    Dim nCount As Integer
    nCount = 0
        For Each x In db.TableDefs(strTableName).Fields
            wrk.BeginTrans
            If db.TableDefs(strTableName).Fields(nCount).Name = strFieldToRename Then    '***This will rename Fields
                db.TableDefs(strTableName).Fields(nCount).Name = strFieldNewName
            End If
            nCount = nCount + 1
            wrk.CommitTrans
        Next
        
'*** This will add a new field
    If strFieldFromScratch <> "" Then
        wrk.BeginTrans
        db.TableDefs(strTableName).Fields.Append tdfNew.CreateField(strFieldFromScratch, dbLong)
        wrk.CommitTrans
    End If
        
    db.Close
    wrk.Close
    
    Set dbEng = Nothing
    Set wrk = Nothing
    Set db = Nothing
    Set tdfNew = Nothing
End Function

Private Sub Command1_Click()
sbPrepareFields "tblInvoice", "SomeName", "SomeNameNew", "FromScratchNEW"
End Sub
Reply With Quote
  #14  
Old 01-18-2002, 06:21 AM
pisoftwar pisoftwar is offline
Centurion
 
Join Date: Nov 2001
Location: Trinidad
Posts: 142
Default

Thats similar to the ADOX version that I am using.
I prefer ADO.
__________________
K.I.S. Keep It Simple.
Reply With Quote
  #15  
Old 01-18-2002, 07:20 AM
diver diver is offline
Senior Contributor

* Expert *
 
Join Date: Jun 2001
Location: Illinois
Posts: 865
Default

Pretty interesting.

I'd like to try this out, I'm assuming that I do not need any special "references" initialized in the project.

The second response spoke of using ADO instead.

I'm also more familiar with ADO.

Thanks!
Diver
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->