 |
 |

01-17-2002, 04:36 PM
|
|
Senior Contributor
* Expert *
|
|
Join Date: Jun 2001
Location: Illinois
Posts: 865
|
|
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
|
|

01-17-2002, 04:46 PM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
Big maybe.
Code:
Insert Into SalesTemp (field1) (select field1 from Sales)
|
|

01-17-2002, 05:07 PM
|
|
Senior Contributor
* Expert *
|
|
Join Date: Jun 2001
Location: Illinois
Posts: 865
|
|
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
|
|

01-17-2002, 05:15 PM
|
|
Senior Contributor
* Expert *
|
|
Join Date: Jun 2001
Location: Illinois
Posts: 865
|
|
|
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
|
|

01-17-2002, 05:29 PM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
|
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.
|
|

01-17-2002, 05:35 PM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
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! 
|
|

01-17-2002, 05:36 PM
|
|
Centurion
|
|
Join Date: Nov 2001
Location: Trinidad
Posts: 142
|
|
|
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.
|

01-17-2002, 05:38 PM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
|
But there is no way to rename a column using alter table alter
column. That was diver's question.
|
|

01-17-2002, 05:41 PM
|
|
Centurion
|
|
Join Date: Nov 2001
Location: Trinidad
Posts: 142
|
|
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.
|

01-17-2002, 06:01 PM
|
|
Senior Contributor
* Expert *
|
|
Join Date: Jun 2001
Location: Illinois
Posts: 865
|
|
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 
|
|

01-17-2002, 06:03 PM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
I told you it was a big maybe. I learn as much from these questions
as you do, that is why I like them. 
|
|

01-17-2002, 07:02 PM
|
|
Senior Contributor
* Expert *
|
|
Join Date: Jun 2001
Location: Illinois
Posts: 865
|
|
|
Okay guys...
So...
No renaming of columns allowed.
Renaming of TABLES allowed?
Diver
|
|

01-17-2002, 09:19 PM
|
 |
Code Factory
Retired Moderator * Expert *
|
|
Join Date: Jan 2001
Location: Montreal, Ca.
Posts: 5,565
|
|
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
|
|

01-18-2002, 06:21 AM
|
|
Centurion
|
|
Join Date: Nov 2001
Location: Trinidad
Posts: 142
|
|
|
Thats similar to the ADOX version that I am using.
I prefer ADO.
|
__________________
K.I.S. Keep It Simple.
|

01-18-2002, 07:20 AM
|
|
Senior Contributor
* Expert *
|
|
Join Date: Jun 2001
Location: Illinois
Posts: 865
|
|
|
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
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|