SendKeys from form and loop trough records (1 Viewer)

MushroomKing

Registered User.
Local time
Today, 07:54
Joined
Jun 7, 2018
Messages
100
Hello dear people of Access world!

I have a question concerning passing data from a form with Sendkeys and looping until the last record.

- So i have a table with measurements and weights.
- I have a form based on this table with all the records.
- Access will open a program that we use and pass data with SendKeys
(for now i use notepad to see what happens fysically)

It has to take the first record and pass it through, then the procedure needs to loop until the last record and the code ends with a MsgBox or something like that.

What the best approach for looping through these records?
Thanks in advance guys!

Code:
Private Sub Command0_Click()

MsgBox "Please don't use the keyboard or mouse during the process. Thanks!"

AppActivate "notepad"

'first start ======================
SendKeys "{7}"
SendKeys "{ENTER}"
SendKeys "{ENTER}"
SendKeys Me.length
SendKeys "{ENTER}"
SendKeys Me.width
SendKeys "{ENTER}"
SendKeys Me.height
SendKeys "{ENTER}"
SendKeys Me.nw
SendKeys "{ENTER}"
SendKeys Me.bw
SendKeys "{ENTER}"
SendKeys "{ENTER}"
SendKeys "{F12}"
SendKeys "{7}"
SendKeys "{ENTER}"
SendKeys "{ENTER}"
SendKeys "{ENTER}"
'first end ======================

End Sub
 

Minty

AWF VIP
Local time
Today, 14:54
Joined
Jul 26, 2013
Messages
10,354
Send keys will be about as reliable as a chocolate teapot I'm afraid.
Anything that might try and grab windows attention (an virus alert, IM alert, skype call notification etc. etc.) will intercept and then even worse interpret your keystrokes.

You will really need an API .
 

isladogs

MVP / VIP
Local time
Today, 14:54
Joined
Jan 14, 2017
Messages
18,186
Agree with Minty - don't use SendKeys. Slow & utterly unreliable

Here is a simple approach which will create a new text file & populate it or append text to an existing text file

Place this code in a standard module

Code:
Option Compare Database
Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim objFso As Object
Dim logStream As Object
Dim fs As Object

'######################################
'#  Use this to create and append a text file with log entries
'#
'#  Parameters
'#      strLogEntry text to write eg "Hello world!"
'#  Usage
'#      LogEntry " Hello World!"
'#
'#  This will create or add to a file with: Hello World!
'#
'######################################
Sub LogEntry(strLogEntry As String)
  
    AddTextToFile "Full path to text file", strLogEntry
   'e.g. AddTextToFile "c:\TestFiles\Text.txt", strLogEntry
    'Debug.Print strLogEntry
End Sub

Sub AddTextToFile(strLogFile As String, strLogEntry As String)

'Identical to WriteToFile but without the date/time
    On Error Resume Next
    
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set logStream = objFso.OpenTextFile(strLogFile, ForAppending, True) 'Open log file
    
    logStream.writeline strLogEntry
    
    logStream.Close
End Sub

Example usage:
AddTextToFile CurrentProject.Path & "\test.txt", "Hello World"

You can also enter the path in LogEntry and save repeated typing
LogEntry "Goodbye"

Each entry will be added to a new line



If you wish, the current time can be added to each new line
 

Attachments

  • Capture.PNG
    Capture.PNG
    3.2 KB · Views: 406

MushroomKing

Registered User.
Local time
Today, 07:54
Joined
Jun 7, 2018
Messages
100
Thanks guys.

But im afraid my hands are bound. I cannot create text files or save them to any location on this network. I only have a warehouse interface and an "approved Access application in my case".

I would just like to try this first.
So how can i move from the first record on my form to the last?

Cheers
 

isladogs

MVP / VIP
Local time
Today, 14:54
Joined
Jan 14, 2017
Messages
18,186
Sorry - I thought you wanted to create a text file!!!

As for your question, I've no idea - I NEVER use SendKeys
 

MushroomKing

Registered User.
Local time
Today, 07:54
Joined
Jun 7, 2018
Messages
100
:) Alright.

Well i use a text file on my own (development) laptop right now.
Just to see if the strokes work at all.

Seems to be fine.

I just need to loop through all the records on the form.
 

JHB

Have been here a while
Local time
Today, 15:54
Joined
Jun 17, 2012
Messages
7,732
Do you need to loop, else you can use <Ctrl> + <End>.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:54
Joined
Sep 21, 2011
Messages
14,044
Something along these lines then perhaps?

The debug line is where you put your code

Code:
Private Sub Command16_Click()
Me.Recordset.MoveFirst
Do Until Me.Recordset.EOF
    With Me.Recordset
        Debug.Print Me.ID
        .MoveNext
    End With
Loop
End Sub
HTH
 

MushroomKing

Registered User.
Local time
Today, 07:54
Joined
Jun 7, 2018
Messages
100
Thanks Gasman!!!!!!

Yes, thats what i was looking for.

Only ONE more thing if you dont mind :D

The first record has a different procedure. And so does the last.
I would like to do something at the first record and then start the loop for the others. Also i want to stop at the second last record.
Because the last record has a different procedure.

I cant find anything on Movenext and second last on Google or the forum.
 

Minty

AWF VIP
Local time
Today, 14:54
Joined
Jul 26, 2013
Messages
10,354
Movefirst positions you on first record. By definition movenext is therefore the "Second" record.

Movelast by definition gets you to one record after the second to last record...

All of this assumes you have 3 or more records in your recordset.
You can store the ID's if these records and check for them in your loop process.
 

MushroomKing

Registered User.
Local time
Today, 07:54
Joined
Jun 7, 2018
Messages
100
Thanks minty :)

Ill try fix that and will come back to it.
I have a strange problem going on aside right now.

Im testing it.
It all seems to work fine.
Just when a value on the form for example is 0,99
It outputs it to notepad as 99

In my query there is 0,99
On my form its 0,99
But on output of the field (SendKeys Me.width) its 99

I tried formatting the query and the form, checked the tables...but to no avail :(
 

JHB

Have been here a while
Local time
Today, 15:54
Joined
Jun 17, 2012
Messages
7,732
Try by converting it to text using the CStr function.
 

MushroomKing

Registered User.
Local time
Today, 07:54
Joined
Jun 7, 2018
Messages
100
Thanks JHB! Sounds like a good solution :)



Dim LValue As String
LValue = CStr(8)


So how would i go about this?

LVALUE = SendKeys Me!width


Like that?
 

JHB

Have been here a while
Local time
Today, 15:54
Joined
Jun 17, 2012
Messages
7,732
Like the below:
Code:
[B]LVALUE = SendKeys [COLOR=red]CStr([/COLOR]Me!Width[COLOR=red])[/COLOR][/B]
 

MushroomKing

Registered User.
Local time
Today, 07:54
Joined
Jun 7, 2018
Messages
100
Thanks for that swift reply!

Code:
LVALUE = SendKeys CStr(Me!Width)


Expected end of statement...


:banghead::D


Code:
LValue = CStr(Me!width)

Like this is ok ofcourse. But i need to send multiple keystrokes :S
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:54
Joined
Sep 21, 2011
Messages
14,044
Code:
LValue = Cstr(Me!Width)
Sendkeys LValue
?
 

JHB

Have been here a while
Local time
Today, 15:54
Joined
Jun 17, 2012
Messages
7,732
How do you send 99 (0,99)?
 

Users who are viewing this thread

Top Bottom