Please help....

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

Hi,

I am struggling with queries or crosstab but it seem that it doesn't work.

I have a table that show:

Category NameCode Name StartDate End Date
******** ********* ***** ******* *******
Group A 345 Name1 20010625 20020304
Group A 345 Name2 20030131 20040606
Group A 345 Name3 20001231 20020722
Group A 345 Name4 20010303 20010331

I just want to make one line from four lines because there are four
different names, four
different startdate

I want to run query like one line:

Category NameCode Name StartDate Name2
StartDate2 -- same with Name3 & 4
******* ********* ***** ********* ****** ********
************
Group A 345 Name1 20010625 Name2 20030131
Name3

Will it be possible and if yes, please help me and provide how to make four
lines into
one lines.

Many thanks, your help would be much appreciated.
 
Hi Bill,

From your descriptions, I understood you would like to make multiple rows
with the same name displayed in one. Have I understood you? If there is
anything I misunderstood, please feel free to let me know :)

Based on my scope, I am afraid there is not an easy way to do so. It's hard
to define the definition of your table, how many column will it to be? The
number column will be changeable according to the same number of nameCode.

I am afraid you have to make another table to restore the data instead of
one query. You need programmacially handle the data, you will need
judgement and selection with these two tables. I am afraid it's very
complex to do so.

Hope it helps and if you have any questions or concerns, don't hesitate to
let me know. We are here to be of assistance!


Sincerely yours,

Mingqing Cheng

Microsoft Developer Community Support
 
Hi Bill,

I was hoping someone would tactfully ask you "Why?"

The query will not be updateable.

If it is for a report, it will suffer from Excel-spread.
The report design that works today will bomb
tomorrow when some group gets that one-more-than
"critical-mass" name pushing data outside your paper
borders. Excel has a printing routine where you
can make it "smaller" so it will print on your paper.

In Access, a main form (or main report) can be bound to

SELECT DISTINCT Category, NameCode
FROM yourtable;

Then a subform (or subform report) can be bound to
your table that shows Name, StartDate, EndDate
in Master/Child relationship with main form's RecordSource...
and in the subform, you can edit/delete/add Name,
StartDate, and EndDate.

Are you doing this so you can export to Excel?

Then, I agree with Mingqing Cheng that one option is to
"make a table." But it will require extaordinary code just
to make the empty table that your data will go in as names
get added to data.

For example, *IF* your field "Name" were not a reserved
word, but instead "AName"; "StartDate" was not a *string*
as it appears, but instead type DateTime; and your table
name were "tblBill":

Public Sub CreateNewTable(strNewTableName As String)
On Error GoTo Err_CreateNewTable
Dim dbs As DAO.Database
Dim tdfNew As DAO.TableDef
Dim rs As DAO.Recordset
Dim strName As String

Set dbs = CurrentDb()

'open recordset to distinct AName's
Set rs = dbs.OpenRecordset("SELECT DISTINCT AName FROM tblBill", dbOpenDynaset)

' Create a new TableDef object.
Set tdfNew = dbs.CreateTableDef(strNewTableName)

With tdfNew
.Fields.Append .CreateField("Category", dbText, 25)
.Fields.Append .CreateField("NameCode", dbLong)
.Fields.Append .CreateField("ID", dbLong)
.Fields("ID").Attributes = .Fields("ID").Attributes + dbAutoIncrField

'create "Name" fields
rs.MoveFirst
Do While Not rs.EOF
strName = rs!AName
'get rid of spaces in name
strName = Replace(strName, " ", "", 1, -1, vbTextCompare)
.Fields.Append .CreateField(strName, dbText, 25)
.Fields.Append .CreateField(strName & "StartDate", dbDate)
rs.MoveNext
Loop
rs.Close
End With

' Append the new TableDef object to the database.
dbs.TableDefs.Append tdfNew


dbs.TableDefs.Refresh
dbs.Close

MsgBox "Have successfully created empty table."

Exit_CreateNewTable:
Set tdfNew = Nothing
Set rs = Nothing
Set dbs = Nothing
Exit Sub
Err_CreateNewTable:
MsgBox Err.Description
Resume Exit_CreateNewTable
End Sub

In above case, you don't get "Name, Name2,.."
for field names, but actual name with spaces removed.
And you don't get "StartDate, StartDate2,..."
for field names, but actual name with space removed
prepended to "StartDate."

Public Sub FillNewTable(strNewTableName As String)
On Error GoTo Err_FillNewTable
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strName As String
Dim strCategory As String
Dim lngNameCode As Long
Dim varStartDate As Variant
Dim strSQL As String

Set dbs = CurrentDb()

'insert distinct Category/NameCode into new table
strSQL = "INSERT INTO " & strNewTableName & "(Category, NameCode) " _
& "SELECT DISTINCT Category, NameCode FROM tblBill;"
dbs.Execute strSQL, dbFailOnError

'open recordset to original table
Set rs = dbs.OpenRecordset("SELECT * FROM tblBill", dbOpenDynaset)

rs.MoveFirst
Do While Not rs.EOF
strName = rs!AName
'get rid of spaces in name
strName = Replace(strName, " ", "", 1, -1, vbTextCompare)
strCategory = rs!Category
lngNameCode = rs!NameCode
varStartDate = rs!StartDate

'insert values
strSQL = "UPDATE " & strNewTableName & " SET " & strName & " = '" _
& strName & "', " & strName & "StartDate = " _
& Format(varStartDate, "\#mm\/dd\/yyyy\#") _
& " WHERE Category = '" & strCategory & "' AND " _
& "NameCode = " & lngNameCode
'Debug.Print strSQL
dbs.Execute strSQL, dbFailOnError

rs.MoveNext
Loop
rs.Close

dbs.Close

MsgBox "Have successfully filled new table."

Exit_FillNewTable:
Set rs = Nothing
Set dbs = Nothing
Exit Sub
Err_FillNewTable:
MsgBox Err.Description
Resume Exit_FillNewTable
End Sub

So...this is an example of how you might
proceed, but you have to wonder if it is
truly necessary, don't you?

Good luck,

Gary Walter
 
Hello Bill,

I was reviewing this thread. Do you still have any more concerns on it? If there is any question, please feel free to post here and we will follow up.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top