Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Insert record into Excel database using a custom VBA form?

Rank Newbie here.
I've initialized a database area in a worksheet, and using
Edit-Define, called the area "database", and am trying to code a
custom form in VBA to insert the data from the form to the database. I
want to add a new record at the bottom of the database list. I don't
know how to code this, or find help on it. I've done lots of this in
SQL but never in VBA to an Excel database/list. Can I have some sample
code for this?

PS. I don't want to use an automatic form (Data-Forms) because a
"current date" field is in the database and I want to generate it
automatically for the user.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default Insert record into Excel database using a custom VBA form?

This assumes that the UF has a text box into which the new entry is typed
named "TextBox1" and that there is a command button named "CommandButton1".
This command button is clicked to add the new entry to the end of the db. The
db is assumed to be in column A of "Sheet1" and all cells below the db in
column A are empty. Therefore, the cell identity for the new entry is
determined by finding the last entry in column A and offsetting 1 cell down.
Note that the named range is therefore unnecessary.

Paste the following to the UF code module:

Dim c As Range
Private Sub UserForm_Activate()
GetNewEntryAddress
End Sub

Private Sub CommandButton1_Click()
c.Value = TextBox1.Text
GetNewEntryAddress
End Sub

Private Sub GetNewEntryAddress()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Set c = ws.Cells(Rows.Count, 1).End(xlUp)(2)
End Sub

" wrote:

Rank Newbie here.
I've initialized a database area in a worksheet, and using
Edit-Define, called the area "database", and am trying to code a
custom form in VBA to insert the data from the form to the database. I
want to add a new record at the bottom of the database list. I don't
know how to code this, or find help on it. I've done lots of this in
SQL but never in VBA to an Excel database/list. Can I have some sample
code for this?

PS. I don't want to use an automatic form (Data-Forms) because a
"current date" field is in the database and I want to generate it
automatically for the user.


  #3   Report Post  
Posted to microsoft.public.excel.programming
pr pr is offline
external usenet poster
 
Posts: 3
Default Insert record into Excel database using a custom VBA form?


wrote:
Rank Newbie here.
I've initialized a database area in a worksheet, and using
Edit-Define, called the area "database", and am trying to code a
custom form in VBA to insert the data from the form to the database. I
want to add a new record at the bottom of the database list. I don't
know how to code this, or find help on it. I've done lots of this in
SQL but never in VBA to an Excel database/list. Can I have some sample
code for this?

PS. I don't want to use an automatic form (Data-Forms) because a
"current date" field is in the database and I want to generate it
automatically for the user.


Assume the following

Insert a new worksheet
Name it Cust
Add 5 Field Headings in A1:E1
In the NameDefine Box enter a new name of DB
in the refers to box enter
=OFFSET(Cust!$A$1,0,0,COUNTA(Cust!$A:$A),5)

Create a new userform
Create 5 textboxes named Textbox1 to Textbox5
Create 2 Command Buttons
First button give a caption of Close and name it CmdClose
Second button give a caption of Add and name it CmdAdd

In the userform add the following code

Option Base 1
Dim NumOfFields As Integer
Dim Ar() As Variant
Dim Dbdata As Range

Private Sub UserForm_Initialize()
Set Dbdata = Range("Db")
NumOfFields = Dbdata.Columns.Count
ReDim Ar(NumOfFields)
End Sub

Private Sub CmdAdd_Click()
Dim I As Integer
Dim NewRecord As Range

Set Dbdata = Range("Db")
For I = 1 To NumOfFields
With Me.Controls("TextBox" & I)
Ar(I) = .Value
.Value = ""
End With
Next
Set NewRecord = Dbdata.Offset(Dbdata.Rows.Count, 0).Resize(1, 5)
NewRecord.Value = Ar()
End Sub

Private Sub CmdClose_Click()
Unload Me
End Sub

Display the userform add data into the textboxes and click
the Add button

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Insert record into Excel database using a custom VBA form?

thanks, guys!

I'll try out the code. Nice to have people respond this way.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Database Conversion (Record Creation) banker123 Excel Discussion (Misc queries) 3 September 22nd 07 01:30 PM
Database Record add Oldjay Excel Discussion (Misc queries) 1 October 13th 06 02:07 AM
How do I email just one record from an excel database. sunbelt vacuum Excel Discussion (Misc queries) 2 August 8th 06 09:53 PM
Record Macro - Record custom user actions Sal[_4_] Excel Programming 1 December 23rd 04 03:18 PM
How do I save a record from an excel template to a database in a . Fred Smith Excel Discussion (Misc queries) 1 November 30th 04 01:49 PM


All times are GMT +1. The time now is 03:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"