+ Reply to Thread
Results 1 to 10 of 10

Send Data from user form to a data base

Hybrid View

  1. #1
    Forum Contributor MariaPap's Avatar
    Join Date
    10-19-2013
    Location
    Chania-Crete-Greece
    MS-Off Ver
    Excel 2003-2010
    Posts
    319

    Send Data from user form to a data base

    Hello.

    I created a simple user form(actually my first one! )in which the user will fill some basics info(name, phone..etc).

    I created a button that calls this user form in a sheet.

    I have no idea how the filled in user form data, should transfer in the first available row in another sheet(database),then clean the user form from the data and do it again.

    Can you help me on this pls?

    Thanks in advance!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Send Data from user form to a data base

    Gali spera Maria.

    This code is over kill for what you want at the moment. But I'd use it anyway as you will need something like this.

    
    Private Sub CommandButton2_Click()
    
    
    Count = 1
    Sheets("DataBase").Select
    
    'Finds the last used row in column A and gets the row number of the cell below that
    NextRow = Range("A1").End(xlDown).Offset(1, 0).Row
    
    'Creates an array
    Dim myArray As Variant
    
    Makes the array big enough to take all your data based on the number of items in the userform
    ReDim myArray(Me.Controls.Count)
    
    
    'This loops through all the items in the userform
    Dim cCont As Control
    
        For Each cCont In Me.Controls
        
    'we are not interested in labels or frames so ignore them
    If TypeName(cCont) <> "Label" And TypeName(cCont) <> "Frame" Then
    
    'Save the value into our array
     myArray(Count) = cCont.Value
    
    increment the count so the next value goes into the next array slot           
    Count = Count + 1
    End If
    10      Next cCont
    
    
    
    'Manipulate columns like this:-   This lets you change the order that the data is written to the spreadsheet
    myArray(0) = myArray(5)
    
    'This line writes all your data to excel.  It is fast and one of the reasons I use an array, the other reason is that it is easy to manipulate the order of the data.
    Range(Cells(NextRow, 1), Cells(NextRow, Me.Controls.Count)).Value = myArray
    
    End Sub

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Send Data from user form to a data base

    Hi, MariaPap,

    give these codes behind the UserForm a try:
    Private Sub CommandButton1_Click()
    Dim lngCount As Long
    For lngCount = 1 To 5
      Controls("Textbox" & lngCount).vlaue = vbNullString
    Next lngCount
    End Sub
    
    Private Sub CommandButton2_Click()
    Dim lngCount As Long
    Dim lngLR As Long
    With Sheets("Database")
      lngLR = .Range("B" & Rows.Count).End(xlUp).Row + 1
      For lngCount = 1 To 5
        .Cells(lngLR, 1 + lngCount).Value = Me.Controls("Textbox" & lngCount)
        Me.Controls("Textbox" & lngCount) = vbNullString
      Next lngCount
    End With
    End Sub
    I´d suggest you place another button on the UF to close the form
    Private Sub CommandButton3_Click()
    Unload Me
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Forum Contributor MariaPap's Avatar
    Join Date
    10-19-2013
    Location
    Chania-Crete-Greece
    MS-Off Ver
    Excel 2003-2010
    Posts
    319

    Re: Send Data from user form to a data base

    Kali spera mehmetcik!

    Thank you for your code. I am sure-as it's my first time that i work with UserForm- that i do something wrong because your code gives me errors(overwrite the results, gives a false value in columns G:H).

    Holger

    Thank you .Your code do exactly what i had in my mind! May i ask 2 questions pls?
    Dim lngCount As Long
    Dim lngLR As Long
    What these 2 lines do?
    .Cells(lngLR, 1 + lngCount).Value = Me.Controls("Textbox" & lngCount)
        Me.Controls("Textbox" & lngCount) = vbNullString
      Next lngCount
    This one -as i understand-control all the text boxes-as i haven't rename these?

    I ask this because my intention is to use another userform to transfer data in columns G:H & K:L, on DataBase sheet. Will i use the same logic for doing this? or it's something completely different?

    Thank you so much!

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Send Data from user form to a data base

    Hi, Maria,

    no problem - there is no comment from my side or text explaining what the Variables are supposed to do.

    Dim lngCount As Long
    This will be used as the counter in the For...Next loop to determine the number of loops to run (here it is from 1 to 5)
    Dim lngLR As Long
    The wrong "terminus techicus" as this would indicate the Last Used Row on a sheet where infact I´m referring to the first free cell in a column (an offset to the last Filled).

    Both Variables are dimmed as Long because of the number of rows in the worksheets and if I remember correctly from Excel2002/XP on all Bytes and Integers are converted into Longs anyway for use in VBA.

        .Cells(lngLR, 1 + lngCount).Value = Me.Controls("Textbox" & lngCount)
        Me.Controls("Textbox" & lngCount) = vbNullString
    You´re right, you didn´t rename the Textboxes so that they can be used with their increasing numbers. As I set a With-Statemenbt before this statement refers to the sheet stated there and will place the values of the textboxes one by one into the very same row starting at column B (which is column number 2: 1 + the lowest number of the loop to run). The next line just clears the contents of the textbox as the data has been written.

    Basicly the logic is identical, you might need an additional Variable to adapt to the diffferent set of Columns which aren´t in straight order after each other, from what you mentioned you will miss I:J.

    Ciao,
    Holger.

  6. #6
    Forum Contributor MariaPap's Avatar
    Join Date
    10-19-2013
    Location
    Chania-Crete-Greece
    MS-Off Ver
    Excel 2003-2010
    Posts
    319

    Re: Send Data from user form to a data base

    Appreciate your feed back with your explanations. I'll try to use these in my new try. If i have problems i know where you are..!

    Thank you once again!

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Send Data from user form to a data base

    Hi Maria

    attached is my version of your workbook.

    I don't get the error you reported.

    I know you have a working solution but my version gives you a lot of powerful tools.

    So it is worth studying.
    Attached Files Attached Files

  8. #8
    Forum Contributor MariaPap's Avatar
    Join Date
    10-19-2013
    Location
    Chania-Crete-Greece
    MS-Off Ver
    Excel 2003-2010
    Posts
    319

    Re: Send Data from user form to a data base

    Hi mehmetcik.

    Thank you. As i told probably i was doing something wrong. Your code works fine. 2-3 questions if you don't mind.

    Can we do something for column G:H that gives us a FALSE value?

    What do we have to change so data to be transfered from row 2 and down-row A will be the headers!

    What if we need to transfer the zip number to column M?

    Thanks again.

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Send Data from user form to a data base

    Hi, mehmetcik,

    but my version gives you a lot of powerful tools.
    Like
    Sheets("DataBase").Select
    or
    temp = TypeName(cCont)
    SCNR

    If TypeName(cCont) <> "Label" And TypeName(cCont) <> "Frame" Then
    will also work for the CommandButtons on the Form, and
    Range(Cells(NextRow, 1), Cells(NextRow, Me.Controls.Count)).Value = myArray
    should rather be
    Range(Cells(NextRow, 1), Cells(NextRow, Count - 1)).Value = myArray
    So it is worth studying.
    Good excercise for error handling/finding.

    Holger

  10. #10
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Send Data from user form to a data base

    [QUOTE=MariaPap;3488541]Hi mehmetcik.

    Can we do something for column G:H that gives us a FALSE value?

    Naí, I have added code to loop through the array and replace "False" with ""

    What do we have to change so data to be transferred from row 2 and down-row A will be the headers!
    Nothing. Just put your titles in row A.

    The macro looks for the first empty row.


    What if we need to transfer the zip number to column M?

    That is your array manipulation.
    M is column 13 which is Array Entry 12 ( Arrays start at 0 )

    so
    Myarray(12) = Myarray(4)
    moves the zip to column M

    and
    Myarray(4) = ""
    clears the old position.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] send data from sheet to user form
    By msaric in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-19-2013, 08:51 PM
  2. send data from worksheet to user form
    By msaric in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-18-2013, 04:08 AM
  3. [SOLVED] Re: Please help for Excel User form send data for multiple worksheets
    By gopala in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-22-2013, 04:08 AM
  4. Web form access data base
    By neeraj_logani in forum Access Tables & Databases
    Replies: 1
    Last Post: 09-24-2012, 04:17 PM
  5. Replies: 17
    Last Post: 05-19-2009, 11:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1