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.
Bright01Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Curt LindstromCommented:
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
0
Bright01Author Commented:
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.
0
Curt LindstromCommented:
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
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Bright01Author Commented:
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.
0
Curt LindstromCommented:
1. This is not a bug. Note the record number at the top right of the form. This is the record you will change. If you want a new record you press New and then enter the values for the new record.

2. Yes you can auto sort it by entering the same code as I use for the button as a worksheet change macro. See attached file.

Cheers,
Curt
Bright2.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bright01Author Commented:
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.
0
Curt LindstromCommented:
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
0
Bright01Author Commented:
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.
0
Bright01Author Commented:
I have closed this question giving Curt the 500 Points for a great answer.  Please credit him with the points.  

B.
0
Curt LindstromCommented:
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
0
Bright01Author Commented:
Yep... Right.

B.
0
Bright01Author Commented:
Great JOB Curt!  Much thanks in helping me with this Macro.

B.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.