Link to home
Start Free TrialLog in
Avatar of Bright01
Bright01Flag for United States of America

asked on

Using Excel as a DB for driving a Model

EE Professionals,

Rob Hanson helped me with a DB Issue with Excel.  I'm trying to build a model that uses a DB; and while Access would be a better choice, Rob showed me how I could take a 4 column DB and by using the "FORMS" function ALT.+D+O in Excel 2010, add to and maintain a DB.  I have two questions;

1.) Can I use this tool via a simple Macro (perhaps a recorded Macro for bringing it up)? -- Example please

and,

2.) Is there some macro code that can insure the order of the DB is automatically maintained?  That is, auto sorted (when a new record is entered) where Col. 1 is the primary, then by Col. 2, then by Col. 3?

Much thanks in advance,

B.
Avatar of Curt Lindstrom
Curt Lindstrom
Flag of Australia image

Try this in attached file

Cheers,
Curt
Sub Button1_Click()
ActiveSheet.ShowDataForm
    Cells.Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A:A"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B:B"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C:C"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A:D")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select

End Sub

Open in new window

Bright1.xlsm
Avatar of Bright01

ASKER

Curt,

Almost!   Your code works in the sheet you provided.  When I transfer it to my workbook I get a "Out of Range" Error 9 and it takes me to this line in the code;

    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear

I've got the sheet named properly in the code, I have the collumns in A:D like in yours starting in A1. The only difference I can see is I'm using text and you are using numerics.  Any ideas?

Thank you,

B.
Try to remove that line. I don't think you need it. It works with or without that line for me.
Where do you run your code from? Sheet2?
Can you post the code you use?

Cheers,
Curt
Curt,

Thank you!  I changed the name to the name of the sheet and it worked....except; with further testing there are two slight bugs;

1.) When the form comes up, and you change something and click "New", it changes the record you are in.....not create a new record.  It seems like some logic doesn't work.  Doesn't new mean a new record?

2.) If a change is made outside using the Button Macro, it doesn't auto-sort.   Is there a way to have it auto-sort if any change is made to any record?

Much thanks,

B.
ASKER CERTIFIED SOLUTION
Avatar of Curt Lindstrom
Curt Lindstrom
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Curt,

Works great!  Since 1 is not a bug,  is there a slight change that allows for the first blank record in Column A (new record) to be where the form starts when opened?  Right now it starts at record 1. Is there an "activiate first blank record" statement or command that allows for when the form is opened that it presents with a blank form or first open record, instead of the first record?

Sorry to scope creep this on you...... I'll close this out when you respond either way.  Much thanks for great work.

B.
Hi B,
Unfortunately we have no control of this form since we're using the built-in form in Excel. To do what you want to do you would have to design your own form which would take quite a bit of time and effort and it's hardly worth the effort just to avoid having to press the button "New" after the form opens.
The line
ActiveSheet.ShowDataForm
opens the built-in form and we have no control in the macro until the form is closed and that's when the records are sorted.

Cheers and Merry Christmas,
Curt
I've requested that this question be closed as follows:

Accepted answer: 0 points for Bright01's comment http:/Q_27498113.html#37308374

for the following reason:

Curt,<br /><br />Much thanks! &nbsp;And Merry XMAS to you.<br /><br />B.
I have closed this question giving Curt the 500 Points for a great answer.  Please credit him with the points.  

B.
I think Bright01 picked his own posting as the answer by mistake. He probably meant to accept my posting ID:37305122 or ID: 37311974 as the answer.

Curt
Yep... Right.

B.
Great JOB Curt!  Much thanks in helping me with this Macro.

B.